2011-01-10 126 views
1

我對我的網頁中的按鈕單擊事件有一個jQuery.ajax調用。這個ajax調用將相當多的標記發送回服務器。經過一些處理後,服務器回覆一個小的URL地址。這有時工作正常,但其他時間沒有。我在ajax調用之前有一個斷點,並且在我的WebMethod中也有一些斷點。看起來有時WebMethod甚至沒有被擊中。jQuery.ajax調用有時不會觸發C#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 
    } 
}); 
+0

你是否能夠一直打到服務,否則如果你沒有發佈任何(空)的方法? – dexter 2011-01-10 15:06:02

+1

檢查jQuery ajax的「錯誤」和「超時」選項,這應該對你有所幫助。另外,如果您調用webservice的方式存在問題,則應該從客戶端收到腳本錯誤。 – 2011-01-10 15:07:59

回答

1

不喜歡回答我自己的問題(不是我真的)。但問題是要處理最大的JSON長度屬性。

我發現answer here

..和將此添加到我的webconfig ...

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

感謝所有答案的傢伙,尤其是那些關於捕獲錯誤。

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

薩爾瓦多Ronnoco,

我建議你添加一個錯誤:回調檢查是怎麼回事。也許你可以從中得到有用的信息。