简体   繁体   English

无法在Ajax调用中在ASPX中调试WebMethod

[英]Unable to debug WebMethod in ASPX on ajax call

I am trying to send a screenshot done via html2canvas to the server using an AJAX call and the POST method, but it's not happening. 我正在尝试使用AJAX调用和POST方法将通过html2canvas完成的屏幕截图发送到服务器,但是这没有发生。 When I am trying to debug the script using Firebug by setting breakpoints at the related lines, my application is not hitting the breakpoint. 当我尝试通过在相关行上设置断点来使用Firebug调试脚本时,我的应用程序未达到该断点。

Here is my client-side code (using jQuery): 这是我的客户端代码(使用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]);
                });

            }
    });

And this is my server-side ASPX WebMethod: 这是我的服务器端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();
        }
    }

When I try to see it within Firebug, its status is 200 OK . 当我尝试在Firebug中查看它时,其状态为200 OK Why is this happening? 为什么会这样呢?

You have invalid JSON: 您的JSON无效:

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

Problem is in your canvas data, Use this one . 问题出在您的画布数据中,请使用此数据。 and just replace the var img = "image"; 只需替换var img =“ image”; variable to as your desire input 可变为您想要的输入

$(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]);
                });


            });
        });

On your client side 在您的客户端

<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" />

On code behind 在后面的代码上

 [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();
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM