web服务上的jquery AJAX请求

问题描述:

我想从与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)/”}]}

调用跨域工作以不同的方式。他们使用回调函数创建插入到页面的动态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文档中有所解释。

希望这会有所帮助。