【问题标题】:Unable to debug WebMethod in ASPX on ajax call无法在 ajax 调用中调试 ASPX 中的 WebMethod
【发布时间】:2014-05-30 22:51:19
【问题描述】:

我正在尝试使用 AJAX 调用和 POST 方法将通过html2canvas 完成的屏幕截图发送到服务器,但它没有发生。当我尝试通过在相关行设置断点来使用 Firebug 调试脚本时,我的应用程序没有遇到断点。

这是我的客户端代码(使用 jQuery):

$("#excel").on("click", function (e) {
        e.preventDefault();

        html2canvas($("#placeholder").get(0), {
            onrendered: function (canvas) {
                //document.body.appendChild(canvas);

                var img = canvas.toDataURL().replace(/^data[:]image\/(png|jpg|jpeg)[;]base64,/i, "");
                $.ajax({
                    "type": "POST",
                    "url": "Default.aspx/MyMethod",
                    "data": {
                        "imageData": img //Send to WebMethod
                    }
                }).done(function (o) {
                    console.log(["Response:", o]);
                });

            }
    });

这是我的服务器端 ASPX WebMethod:

 [WebMethod()]
public static void MyMethod(string imageData)
{

    string fileNameWitPath = "D:/Kabir/custom_name.png";
    using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
    {
        using (BinaryWriter bw = new BinaryWriter(fs))
        {
            byte[] data = Convert.FromBase64String(imageData);//convert from base64
            bw.Write(data);
            bw.Close();
        }
    }

当我尝试在 Firebug 中查看它时,它的状态是 200 OK。为什么会这样?

【问题讨论】:

  • 是否正确传递参数?还要检查整个 url 是否正确并且它没有到达其他地方?你有没有在函数的开头尝试过断点?
  • @user2338488 localhost:4247/JQPlot/Default.aspx/MyMethod 这是我的 URl,参数是正确的
  • @user2338488 我在 console.log(["Response:", o]);在萤火虫中看到时
  • 如果您在 firebug 中查看您的网络选项卡,查找该帖子被调用,并查看该调用的响应是什么。
  • @user2338488 在我的帖子中 imageData 已作为参数传递,在响应中我看到了我的网页 html 内容

标签: c# jquery asp.net ajax firebug


【解决方案1】:

您的 JSON 无效:

                "type": "POST",
                "url": "Default.aspx/MyMethod",
                "data": {
                    "imageData": img //Send to WebMethod
                }

问题出在你的画布数据中,使用这个。只需替换 var img = "image";可变为您的愿望输入

$(document).ready(function () {
            $("#excel").on("click", function (e) {
                e.preventDefault();
                //document.body.appendChild(canvas);
                var img = "image";
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/MyMethod",
                    data: JSON.stringify({ imageData: img }),
                    contentType: "application/json; charset=utf-8"
                }).done(function (o) {
                    console.log(["Response:", o]);
                });


            });
        });

【讨论】:

  • 在 firebug 响应中出现“Unknown web method MyMethod.<br>Parameter name: methodName”错误
  • 问题出在您的画布数据中,请参阅我上面的答案并进行相应更改
  • 但它在我身边工作!让我在下面添加我的代码作为新答案
  • 再次给出错误 "Unknown web method MyMethod.<br>Parameter name: methodName" ...什么可能是错误的其他原因..请帮助我先生..可能需要将某些内容更改为 web.config
  • 可能是。检查您的方法名称,将其设为小写,并验证名称应该相同。区分大小写
【解决方案2】:

在您的客户端

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#excel").on("click", function (e) {
                e.preventDefault();
                //document.body.appendChild(canvas);
                var img = "image";
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/MyMethod",
                    data: JSON.stringify({ imageData: img }),
                    contentType: "application/json; charset=utf-8"
                }).done(function (o) {
                    console.log(["Response:", o]);
                });
            });
        });
    </script>
    <input type="button" id="excel" title="something" />

关于背后的代码

 [WebMethod()]
        public static void MyMethod(string imageData)
        {

            string fileNameWitPath = "D:/Kabir/custom_name.png";
            using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
            {
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    byte[] data = Convert.FromBase64String(imageData);//convert from base64
                    bw.Write(data);
                    bw.Close();
                }
            }
        }

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多