jQuery 如何在没有标题的情况下提取响应内容(仅正文)?使用jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17624447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I extract the response content (only body) without headers? using Jquery
提问by hop
How do I extract the response content (only body) without headers?
如何在没有标题的情况下提取响应内容(仅正文)?
$.ajax({
type: "GET",
url: "http://myRestservice.domain.com",
success: function(data, textStatus, request){
alert(data); //This prints the response with the header.
},
error: function(){
alert('fail');
}
});
The above code prints
上面的代码打印
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 12 Jul 2013 20:24:06 GMT
Content-Length: 232
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"UserID":3,"RoleID":8,"ActivityID":3,"RoleIName":"E",,"Duration":10,"ValidationMsg":"Passed"}</string>
I need to extract the value of ValidationMsg. This is a rest service call.
我需要提取 ValidationMsg 的值。这是一个休息服务电话。
How do I get the response without header informations?
如何在没有标头信息的情况下获得响应?
回答by Parris
I think your server is delivering a content type that you aren't expecting.
我认为您的服务器正在提供您不期望的内容类型。
Steps to resolve:
解决步骤:
- Open up the network tab in chrome's developer tools, watch the request occur and read what content type it is being delivered as. I bet, it is something like
text/plain
ortext/html
. - For JSON your server should be delivering it as
application/json
. - Your ajax request should specify
dataType
as 'json'. Normally $.ajax guesses appropriately; however, since your server is claiming it is text of some sort you are getting headers in your response.
- 在 chrome 的开发人员工具中打开网络选项卡,观察请求的发生并阅读它被传送的内容类型。我敢打赌,它类似于
text/plain
或text/html
。 - 对于 JSON,您的服务器应该将其作为
application/json
. - 您的 ajax 请求应指定
dataType
为“json”。通常 $.ajax 会适当地猜测;但是,由于您的服务器声称它是某种类型的文本,因此您在响应中收到了标头。
回答by James Holderness
I suspect there is something wrong with your server code if you are getting back headers in the dataparameter. The code you have provided works fine for me connecting to a test server returning valid XML - the dataparameter ends up containing an XML document object.
如果您在data参数中取回标头,我怀疑您的服务器代码有问题。您提供的代码对我连接到返回有效 XML 的测试服务器来说工作正常 - data参数最终包含一个 XML 文档对象。
I'd suggest you try opening that url in a browser and see what it returns. Also, if the XML is being generated programatically on the server, you could try just creating a static XML file instead and see if that works any better.
我建议您尝试在浏览器中打开该 url 并查看它返回的内容。此外,如果 XML 是在服务器上以编程方式生成的,您可以尝试只创建一个静态 XML 文件,看看是否效果更好。
Once you have the server returning valid XML, you can extract the string content from the XML object in the dataparameter like this:
一旦您让服务器返回有效的 XML,您就可以从数据参数中的 XML 对象中提取字符串内容,如下所示:
var stringContent = $(data).text();
Then you can parse the JSON from that string content with:
然后您可以从该字符串内容解析 JSON:
var json = $.parseJSON(stringContent);
And finally extract the validationMessagekey with:
最后提取validationMessage键:
var validationMessage = json.ValidationMsg;
This is assuming the JSON in that string element is valid json. However in the example you have given, there is a double comma between "RoleIName" and "Duration" which makes it invalid.
这是假设该字符串元素中的 JSON 是有效的 json。但是,在您给出的示例中,“RoleIName”和“Duration”之间有一个双逗号,这使其无效。
If you can't fix that on the server side, you could possibly fix it on the client side with a simple string replace like this:
如果你不能在服务器端修复它,你可以在客户端用一个简单的字符串替换来修复它,如下所示:
stringContent = stringContent.replace(',,', ',');
This isn't a particularly safe thing to do in general, but if you're not worried about having commas in the json content that could be corrupted by such a call, it shouldn't be an issue.
一般来说,这不是一件特别安全的事情,但是如果您不担心 json 内容中的逗号可能会被此类调用损坏,那么这应该不是问题。
Putting that all together, the final successfunction should look something like this:
综上所述,最终的成功函数应如下所示:
success: function(data, textStatus, request){
var stringContent = $(data).text();
stringContent = stringContent.replace(',,', ',');
var json = $.parseJSON(stringContent);
var validationMessage = json.ValidationMsg;
/* do whatever you need with the validationMessage here */
},
Here's a codepen link demonstrating the working script: http://codepen.io/anon/pen/LeDlg
这是一个演示工作脚本的 codepen 链接:http://codepen.io/anon/pen/LeDlg
回答by carlosherrera
My idea is to try using contentType in your ajax call:
我的想法是尝试在您的 ajax 调用中使用 contentType:
$.ajax({
type: "GET",
url: "http://myRestservice.domain.com",
**contentType: "application/json",**
success: function(data, textStatus, request){
alert(data); //This prints the response with the header.
},
error: function(){
alert('fail');
}
});
and then try to catch the json object
然后尝试捕获 json 对象