我正在尝试使用DOJO库从Javascript进行跨域调用。我正在调用的Web服务返回JSON 我为此目的使用“dojo.io.script.get”。 Web服务没有为callbackParamName建立任何特定的查询字符串参数,所以我使用ar任意名称,如“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);
答案 0 :(得分:0)
您所描述的内容听起来像JSONP。因此,服务器需要以JSONP格式发送数据,而不是JSON。
Dojo注入的脚本中存在语法错误,因为dojo.script.io
期望服务器以此格式返回结果:
callback({"GeocodeAddressResponse": "blah blah blah"});
说明:内部Dojo构造一个名为“callback”的函数,因此它可以执行从服务器发送的JSONP。此函数处理数据并将其发送到load
函数。通过尝试以this example之类的JSONP格式发送数据的服务器进行验证(http://ajax.googleapis.com/ajax/services/search/web)。
<强>解决方法:强>
如果无法控制跨域服务器返回的格式,则必须在同源服务器上设置跨域代理,或者配置浏览器以允许跨域AJAX调用。 / p>
您可能还想调查Cross-Origin Resource Sharing (CORS)这是一个新的,更安全的标准,旨在取代JSONP。