Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xmlhttprequest responsetext coming for Accept header: text/xml , but server error for application/JSON

I have to get response text from a resourceindex page as JSON object. When I dont put a Accept header in the request, it shows me the xml response (i see it in an alert)..

But I want the response as a JSON object.. What should I do. One solution would have been

httpRequest.setRequestHeader('Accept', 'application/JSON');

but this gives me a server error :500

Also it says

A message body writer for Java type, class ...., and MIME media type, application/octet-stream, was not found

Can someone suggest on what to do to overcome this and get the response as JSON?

like image 919
encryptor Avatar asked May 13 '10 06:05

encryptor


People also ask

What is header (' Content-Type application json ');?

Content-Type: application/json is just the content header. The content header is just information about the type of returned data, ex::JSON,image(png,jpg,etc..),html. Keep in mind, that JSON in JavaScript is an array or object.

What HTTP header is used to indicate that json is being used as the payload?

A PUT request header.

How can request Content-Type be set to XML via XMLHttpRequest?

setRequestHeader('Content-Type', 'application/json') ; is added 1 line above or below the Accept header, the method used changes to OPTIONS, the Accept header changes to "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8" and the Content-Type header disappears as if it wasn't seen.


2 Answers

The correct mime type for JSON is application/json, not application/JSON. That might be the problem.

like image 56
T.J. Crowder Avatar answered Oct 22 '22 09:10

T.J. Crowder


httpRequest.setRequestHeader('Accept', 'application/json'); 

The MIME type is json and not JSON.

like image 36
encryptor Avatar answered Oct 22 '22 08:10

encryptor