Skip to main content
 首页 » 编程设计

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

2024年02月23日36jillzhang

我在我的网页中有一个按钮点击事件的 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 
    } 
});