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.
How do I get the response without header informations?
I think your server is delivering a content type that you aren't expecting.
text/plain
or text/html
.application/json
.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.I suspect there is something wrong with your server code if you are getting back headers in the data parameter. The code you have provided works fine for me connecting to a test server returning valid XML - the data parameter ends up containing an XML document object.
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.
Once you have the server returning valid XML, you can extract the string content from the XML object in the data parameter like this:
var stringContent = $(data).text();
Then you can parse the JSON from that string content with:
var json = $.parseJSON(stringContent);
And finally extract the validationMessage key with:
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.
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.
Putting that all together, the final success function 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With