我在我的网页上的按钮点击事件上有一个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 } });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 } });最满意答案
我建议你像这样重写你的方法:
$.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 } });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.
更多推荐
发布评论