对我的 Web 服务的 Ajax 查询在我的 json 中返回 xml - 第 2 部分
又是我(上一个问题 )我仍然遇到从 ajax 调用返回 json 和 xml 的问题。
我在 MonoDevelop 2.2 版中编写了一个 Web 服务来返回我的 json。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getLocationJ(){}
返回:-
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(bigPM);
return json;
如果我测试我的网络服务,我会得到:-
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">[{"placeName":"XXXX","address":"Some Address","lat":12121,"lng":12121}]</string>
这正是我在进行 ajax 调用时所引入的内容。我的 json 仍然封装在 XML 中,因此无法读取。
这是我的 ajax 调用:-
$.ajax({
type: "GET",
url: theURL,
async: true,
data: {minLong:minLon, maxLong:maxLon, minLat:minLat, maxLat:maxLat},
cache: false,
dataType: "jsonp",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert('in here');
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
alert(xhr.statusText);
}
});
如果我只执行 json,我会收到 500 内部服务器错误,如果我执行 POST,我会收到 403 禁止错误。
今天早上我尝试这样做:-
$.getJSON(theURL, {minLong:minLon, maxLong:maxLon, minLat:minLat, maxLat:maxLat}, function(data) {
);
});
只有我遇到了完全相同的问题。
如果我可以从 json 中删除 xml,那么我就可以继续前进,但现在我已经死在水里了,我想我快被 ajax 淹没了!
请帮忙 谢丽尔
It's me again (previous question) I am still having problems with json and xml being returned from an ajax call.
I have written a webservice in MonoDevelop version 2.2 to return my json.
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getLocationJ(){}
Which returns:-
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(bigPM);
return json;
If I test my webservice I get:-
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">[{"placeName":"XXXX","address":"Some Address","lat":12121,"lng":12121}]</string>
Which is exactly what I am pulling in when I make my ajax calls. My json is still wrapped in XML and therefore cannot be read.
This is my ajax call:-
$.ajax({
type: "GET",
url: theURL,
async: true,
data: {minLong:minLon, maxLong:maxLon, minLat:minLat, maxLat:maxLat},
cache: false,
dataType: "jsonp",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert('in here');
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
alert(xhr.statusText);
}
});
If I do just json I get 500 Internal server error, if I do a POST I get 403 forbidden error.
This morning I tried doing:-
$.getJSON(theURL, {minLong:minLon, maxLong:maxLon, minLat:minLat, maxLat:maxLat}, function(data) {
);
});
Only I get the exact same problems.
If I could just remove the xml from my json then I could move forward but right now I am dead in the water and I think I am drowning in ajax!
Please help
Cheryl
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要在 WebMethod 中返回字符串,而是返回 void 并使用:
Instead of returning a string in your WebMethod, return void and use:
一个快速但肮脏的修复方法是从 success 函数中的 xml 中提取 json。
A quick and dirty fix is to extract your json from the xml in the success function.
更改
为
完整示例:
Web 服务方法的签名:
change
to
Complete example:
signature of the web service method:
确保您的服务类具有 [ScriptService] 属性。默认情况下不添加该属性。
Make sure your service class has [ScriptService] attribute. This attribute is not added by default.