0

我正在尝试使用 DOJO 库从 Javascript 进行跨域调用。我正在调用的 Web 服务返回 JSON。
为此,我正在使用“dojo.io.script.get”。Web 服务没有为 callbackParamName 建立任何特定的查询字符串参数,因此我使用的是任意名称,例如“callback”。DOJO 将注入 SCRIPT 标记,其结果如下所示(从 Firebug 中提取):

<script id="dojo_request_script0" type="text/javascript" src="http://localhost:8281/services/TestGeocodeWorldLocator.TestGeocodeWorldLocatorHttpSoap12Endpoint?format=json&callback=dojo.io.script.jsonp_dojoIoScript1._jsonpCallback" async="" charset="utf-8">

{"GeocodeAddressResponse":{"Result":{"PropertyArray":{"PropertySetProperty":[{"Key":"Shape","Value":{"X":"-8841758.9684124179","Y":"5474103.2948672064","SpatialReference":{"WKT":"PROJCS[\"WGS_1984_Web_Mercator_Auxiliary_Sphere\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Mercator_Auxiliary_Sphere\"],PARAMETER[\"False_Easting\",0.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",0.0],PARAMETER[\"Standard_Parallel_1\",0.0],PARAMETER[\"Auxiliary_Sphere_Type\",0.0],UNIT[\"Meter\",1.0],AUTHORITY[\"EPSG\",3857]]","XOrigin":"-20037700","YOrigin":"-30241100","XYScale":"10000","ZOrigin":"-100000","ZScale":"10000","MOrigin":"-100000","MScale":"10000","XYTolerance":"0.001","ZTolerance":"0.001","MTolerance":"0.001","HighPrecision":"true","WKID":"3857"}}},{"Key":"Status","Value":"M"},{"Key":"Score","Value":"100"},{"Key":"Match_addr","Value":"1145 Nicholson Rd, Newmarket, ON, L3y"},{"Key":"PreType","Value":""},{"Key":"City","Value":"NEWMARKET"},{"Key":"Addr_type","Value":"StreetAddress"},{"Key":"X","Value":"-79.426873000000001"},{"Key":"Y","Value":"44.055940999999997"},{"Key":"Side","Value":"R"},{"Key":"House","Value":"1145"},{"Key":"PreDir","Value":""},{"Key":"StreetName","Value":"NICHOLSON"},{"Key":"SufType","Value":"RD"},{"Key":"SufDir","Value":""},{"Key":"Province","Value":"ON"},{"Key":"Postal","Value":"L3Y"},{"Key":"Disp_Lon","Value":""},{"Key":"Disp_Lat","Value":""},{"Key":"Loc_name","Value":"CAN_Streets"}]}}}}

</script>

问题是浏览器(IE 或 Firefox)抱怨“语法错误 - 预期为“;””。基本上它不喜欢在 SCRIPT 标签中注入的 JSON 响应中的冒号“:”。Codewise,Javascript 在这一行崩溃: load: function(response, ioArgs)

我认为这可能与 callbackParamName 有关系...但服务器不需要任何特定名称。有人可以建议我如何解决这个问题吗?

这是我正在使用的代码:

<head>
<script type="text/javascript" src='dojo-release-1.8.0-src/dojo/dojo.js' data-dojo-config='parseOnLoad: true, isDebug:true'></script>

<script type="text/javascript">

dojo.require("dojo.io.script");

   function DOJOtoWS() {

       var targetNode = dojo.byId("results");

       var jsonpArgs = {

         url: "http://localhost:8281/services/TestGeocodeWorldLocator.TestGeocodeWorldLocatorHttpEndpoint",             
         callbackParamName: "callback",             
         content: {format : "json"},                         
         load: function(response, ioArgs){

           console.log(response);
           return response;

           // Set the data from the search into the viewbox in nicely formatted JSON
           targetNode.innerHTML = "<pre>" + dojo.fromJson(response) + "</pre>";

         },
         error: function(response, ioArgs){
           targetNode.innerHTML = "An unexpected error occurred: " + response;

           console.log("error");
           console.log(response);
           return response;
         }
       };
       dojo.io.script.get(jsonpArgs); 
  }

  dojo.ready(DOJOtoWS);


4

1 回答 1

0

你所描述的听起来像JSONP。因此服务器需要以 JSONP 格式发送数据,而不是 JSON。

Dojo 注入的脚本中存在语法错误,因为dojo.script.io期望服务器以这种格式返回结果:

callback({"GeocodeAddressResponse": "blah blah blah"});

说明: Dojo 在内部构造了一个名为“回调”的函数,因此它可以执行从服务器发送的 JSONP。此函数处理数据并将其发送到您的load函数。通过尝试以 JSONP 格式发回数据的服务器进行验证,如本示例(http://ajax.googleapis.com/ajax/services/search/web)。

解决方法:

如果您无法控制跨域服务器返回的格式,则必须在同源服务器上设置跨域代理或将浏览器配置为允许跨域 AJAX 调用。

您可能还想研究跨域资源共享 (CORS),它是一种新的、更安全的标准,旨在取代 JSONP。

于 2012-08-30T19:17:11.220 回答