简体   繁体   English

jQuery.ajax调用有时不会触发C#WebMethod

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

I have a jQuery.ajax call on a button click event in my webpage. 我在我的网页上的按钮点击事件上有一个jQuery.ajax调用。 This ajax call sends quite a lot of markup back to the server. 这个ajax调用将很多标记发送回服务器。 After some processing the server posts back a small URL address. 经过一些处理后,服务器回发一个小的URL地址。 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. 我在ajax调用之前有一个断点,并且在我的WebMethod中也有一些断点。 It appears that sometimes the WebMethod doesn't even get hit. 似乎有时WebMethod甚至没有被击中。

What could be causing the .ajax call to fail? 什么可能导致.ajax调用失败? I'm assuming there must be something in the parameters I am sending. 我假设我发送的参数必须有一些东西。 But I am escape ing the markup. 但我正在escape加价。

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, 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. 但问题与最大JSON长度属性有关。

I found the answer here 我在这里找到了答案

..and added this to my webconfig... ..并将其添加到我的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. 感谢所有答案的人,特别是那些关于捕捉错误的人。

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

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