Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract the response content (only body) without headers? using Jquery

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?

like image 486
hop Avatar asked Sep 03 '25 02:09

hop


2 Answers

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 or text/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.
like image 148
Parris Avatar answered Sep 04 '25 22:09

Parris


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

like image 43
James Holderness Avatar answered Sep 04 '25 22:09

James Holderness