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

标签 c# jquery asmx

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

什么可能导致 .ajax 调用失败?我假设我发送的参数中一定有一些东西。但我正在转义标记。

有人有什么想法吗?

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

关于c# - jQuery.ajax 调用有时不会触发 C# WebMethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4648126/

相关文章:

c# - 使用 Linq 读取第 n 层列表并将所需数据保存到另一个列表

c# - 检查 lambda 表达式中的属性是否为 null

javascript - 选择一行时用文本填充下拉字段

jquery过滤有+没有

javascript - Angular Bootstrap 单选按钮不触发功能

c# - 通过 javascript 将复杂对象传递到 .Net 服务

c# - Webservice复杂类型和类继承

c# - 如何将 json 字符串(复杂对象)转换为 NameValueCollection?

c# - JsonConverter 不会用破折号(连字符)转换枚举

wcf - 通过 WCF 路由服务将调用路由到旧版 ASMX Web 服务