我正在尝试使用 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);