jQuery.ajax调用有时不会触发C#WebMethod(jQuery.ajax call sometimes doesn't fire C# WebMethod)

编程入门 行业动态 更新时间:2024-10-27 16:32:10
jQuery.ajax调用有时不会触发C#WebMethod(jQuery.ajax call sometimes doesn't fire C# WebMethod)

我在我的网页上的按钮点击事件上有一个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.

更多推荐

本文发布于:2023-08-01 10:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357770.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ajax   jQuery   WebMethod   fire   call

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!