2012-02-14 115 views
0

我想從與jquery.here客戶消耗跨域web服務不返回值是我的代碼web服務上的jquery AJAX請求

功能的getId(){

 var testid = ($('#<%=PreviousTest.ClientID %> OPTION:selected').val()); 

     jQuery.support.cors = true; 
     jQuery.ajax({ 
      type: "POST", 
      url: "../FalconWebService.asmx/minlatency", 
      data: "{'testId':" + testid + "}", 
      contentType: "application/json; charset=utf-8", 
      dataType:"json", 
      success: function (data) { 
       alert("catch"); 
       var msg = jQuery.parseJSON(data.Table); 
       return msg; 

       }, 
      Error: function() { 
       alert("error"); 
      } 

我的web服務的回報以下格式的值
{「Table」:[{「minlatency」:16.0,「Time」:「/ Date(1328248782660 + 0530)/」},{「minlatency」:7.0,日期(1328248784677 + 0530)/ 「},{」 minlatency 「:13.0,」 時間 「:」/日期(1328248786690 + 0530)/ 「},{」 minlatency 「:6.0,」 時間 「:」/日期(1328248788690+ 0530)/ 「},{」 minlatency 「:20.0,」 時間 「:」/日期(1328248790707 + 0530)/ 「},{」 minlatency 「:12.0,」 時間 「:」/日期(1328248792723 + 0530)/ 「},{」 minlatency「:26.0, 「Time」:「/ Date(1328248794723 + 0530)/」},{「minlatency」:18.0,「Time」:「/ Date(1328248796723 + 0530)/」}]}

回答

0

調用跨域工作以不同的方式。他們使用回調函數創建插入到頁面的動態javascript。該功能用於處理來自服務的響應。 Jquery調用添加「?callback =?」到服務的URL,其中「回調」是將要插入的函數的名稱。

若要調用跨域服務使用jQuery,你必須做到以下幾點:

jQuery.ajax({ 
    type: "POST", 
    url: "../FalconWebService.asmx/minlatency", 
    data: "{'testId':" + testid + "}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "jsonp", //The data type that you must use is JSONP. Basically tells JQuery that the request is cross-domain 
    success: function (data) { 
     alert("catch"); 
     var msg = jQuery.parseJSON(data.Table); 
     return msg; 
    }, 
    Error: function() { 
     alert("error"); 
    }, 
    jsonpCallback: 'callback' //Dude to the fact that the JS is being generated dynamicaly, this tells JQuery to use the name "callback" for the function that will handle the result, as it adds "?callback=?" to the URL 
}); 

,如果你想「覆蓋一個回調函數的名字你也可以使用「JSONP」而不是「jsonpCallback」 jsonp請求「(來自JQuery)。

它爲我工作(在與JSON消息通信的WCF REST服務中,但它應該以與您的情況相同的方式)。這在JQuery Ajax文檔中有所解釋。

希望這會有所幫助。