dojo newbie从web服务读取json响应 [英] dojo newbie read json response from web service
问题描述
我有一个webservice返回这个响应:
i have a webservice that is returning this response :
<string xmlns="http://tempuri.org/">{ "Head":[ { "ID":"1","Name":"David"} ]}</string>
当我尝试得到回复,我不断收到错误:
丢失; before statement
when i try to get the response back, i keep getting the error : "missing ; before statement"
我刚刚开始进入这个,所以我可能会做错事。
i am just starting to get into this so am probably doing something very wrong.
为什么回应不适合我?
我的dojo代码看起来像这样
my dojo code looks like this
var targetNode = document.getElementById("foo");
var def = dojo.io.script.get({
url: "http://localhost/WebData/PublicData.asmx/HelloWorld",
timeout: 30000,
handleAs: "json",
preventCache: true,
handle: function(error, ioargs) {
var message = "";
switch (ioargs.xhr.status) {
case 200:
message = "Good request.";
break;
case 404:
message = "The requested page was not found";
break;
case 500:
message = "The server reported an error.";
break;
case 407:
message = "You need to authenticate with a proxy.";
break;
default:
message = "Unknown error.";
}
targetNode.innerHTML = message;
}
});
谢谢!
david
thanks! david
推荐答案
您的服务器的响应混合了类似XML的数据(< string xmlns = http://tempuri.org/\">
)与JSON。
Your server's response mixes XML-like data (<string xmlns="http://tempuri.org/">
)with JSON.
对于dojo,使用 handleAs:'json'
处理响应,您将需要您的服务器返回纯JSON数据只有
For dojo to handle the response using handleAs: 'json'
you will need your server to return pure JSON data only, i.e just
{ "Head":[ { "ID":"1","Name":"David"} ]}
如果您需要处理响应作为文本,请删除标签,然后解析JSON内容。作为一般提示丢失;之前的声明通常意味着错误的JSON。
Failing that you would need to handle the response as text, strip out the tags and then parse just the JSON content. As a general tip "missing ; before statement" usually means mal-formed JSON.
编辑
注意到你的handle函数的第一个参数是error。 handle函数的第一个参数包含服务器的响应(在这种情况下是基于收到的JSON的JavaScript对象)。
I just noticed the first argument to your handle function is "error". The first argument to the handle function contains the server's response (In this case a javascript object based on the JSON received.).
这篇关于dojo newbie从web服务读取json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!