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

时间:2011-01-10 15:00:40

标签: c# jquery asmx

我在网页上的按钮点击事件中调用了jQuery.ajax。这个ajax调用将很多标记发送回服务器。经过一些处理后,服务器回发一个小的URL地址。这有时很好,但其他时候没有。我在ajax调用之前有一个断点,并且在我的WebMethod中也有一些断点。看来WebMethod有时甚至不会被击中。

什么可能导致.ajax调用失败?我假设我发送的参数必须有一些东西。但我是escape标记。

有人有任何想法吗?

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

3 个答案:

答案 0 :(得分:2)

我建议你像这样重写你的方法:

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

答案 1 :(得分:1)

El Ronnoco,

我建议你添加一个错误:回调来检查发生了什么。也许你可以从中获得有用的信息。

答案 2 :(得分:1)

不喜欢回答我自己的问题(不是我,真的)。但问题在于最大JSON长度属性。

我找到了answer here

..并将其添加到我的webconfig ...

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

感谢所有答案的人,特别是那些关于捕捉错误的答案。