开发者

jQuery.ajax call sometimes doesn't fire C# WebMethod

开发者 https://www.devze.com 2023-02-03 05:43 出处:网络
I have a jQuery.ajax call on a button click event in my webpage. This ajax call sends quite a lot of markup back to the server. After some processing the server posts back a small URL address. This wo

I have a jQuery.ajax call on a button click event in my webpage. This ajax call sends quite a lot of markup back to the server. After some processing the server posts back a small URL address. This works fine sometimes but other times doesn't. I have a breakpoint just before the ajax call and also have some in my WebMethod. It appears that sometimes the WebMethod doesn't even get hit.

What could be causing the .ajax call to fail? I'm assuming there must be something in the parameters I am sending. But I am escapeing the markup.

Anyone got any ideas?

$.ajax({
    type: 'POST',
    url: 'WebServices.asmx/GetBitmapPathForVML',
    contentType: 'application/json; charset=utf-8',
    data: '{"sVML" : "' + escape($('#divChart')[0].innerHTML) + 
      '","width" : 800,"height": 600}',
    dataType: 'json',
    success: function(result) {
            var newWindow = window.open ("", "Chart","");
            //blah blah
            newWindow.document.write("<BODY>");
          开发者_如何学编程  newWindow.document.write(
              '<img src="file" alt="Chart"></img>'.replace('file',result.d)
            );  
            newWindow.document.write("</BODY>");    
            //blah blah
    }
});  


I would recommend you rewriting your method like this:

$.ajax({
    type: 'POST',
    url: 'WebServices.asmx/GetBitmapPathForVML',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({
        sVML: $('#divChart').html(),
        width: 800,
        height: 600
    }),
    dataType: 'json',
    success: function(result) {
        var newWindow = window.open ("", "Chart","");
        //blah blah
        newWindow.document.write("<BODY>");
        newWindow.document.write(
            '<img src="file" alt="Chart"></img>'.replace('file',result.d)
        );  
        newWindow.document.write("</BODY>");    
        //blah blah
    }
}); 


El Ronnoco,

I would suggest you to add a error: callback to check what is going on. Maybe you can get usefull information from that.


Don't like answering my own question (not that I am, really). But the issue was to do with the maximum JSON length property.

I found the answer here

..and added this to my webconfig...

<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="2097152"/>
        </webServices>
    </scripting>
</system.web.extensions>    

Thanks for all the answers guys, especially those about catching the errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消