使用jquery($ .ajax)连接到Web服务

时间:2012-01-01 21:41:49

标签: javascript jquery ajax

我尝试连接到此网络服务:

  

http://www.w3schools.com/webservices/tempconvert.asmx

但它警告“错误”

这是我使用的脚本:

     var varType;
     var varUrl;
     var varData;
     var varContentType;
     var varDataType;
     var varProcessData;
     function SetValue() {
         varType = "POST";
         varUrl = "http://www.w3schools.com/webservices/tempconvert.asmx/FahrenheitToCelsius";
         varData = '{"Fahrenheit":"230"}';
         varContentType = "application/json; charset=utf-8";
         varDataType = "json";
         varProcessData = true;
         CallService();
     }
     function CallService() {
         $.ajax({
             type: varType, 
             url: varUrl, 
             data: varData, 
             contentType: varContentType, 
             dataType: varDataType, 
             processdata: varProcessData, 
             success: function (msg) {
                 ServiceSucceeded(msg);
             },
             error:function (xhr, ajaxOptions, thrownError){
                 alert(xhr.statusText);
                alert(thrownError);
            } 
         });
     }

     function ServiceSucceeded(result) {
         alert("ServiceSucceeded");

         varType = null; varUrl = null; varData = null; varContentType = null; varDataType = null; varProcessData = null;
     }

我该怎么办?

1 个答案:

答案 0 :(得分:1)

由于dataType: 'jsonp'

,您在执行跨域请求时必须使用same-origin policy