0

I am making an ajax call , here I am not sending any data I am fetching the data in response whcih was already set by previous request. On the server side I am building jsonObject and sending , what is the contentType I should use application/x-json or text/x-json as all my datas are text.

Annapoorni D
  • 681
  • 2
  • 10
  • 30

2 Answers2

2

This should be easy to search for you, but anyway: What is the correct JSON content type?

(TL;DR: The MIME media type for JSON text is application/json)

Community
  • 1
  • 1
Christoffer
  • 2,055
  • 15
  • 21
  • yes I came acros that SO question but I was confused as one answer mentions depending on what your application sends , if it is text "text/x-json" this can be used. – Annapoorni D Nov 16 '12 at 09:35
1

On server side you should return :MediaType.APPLICATION_JSON (if you are using jersey java).

On client side(js) you should use datatype:"JSON". (Not content type , because content type is used to specify type of data you are sending to server and datatype is the type of data you are expecting from server in response).

Example:

1. Server Side (jersey java- JAX-RS):

@GET
@Path("/yourpath.json")
@Produces(MediaType.APPLICATION_JSON)
public returnSomething functionName(){
    ...
    ...
}

2. Client Side (ajax call):

$.ajax({
type: "GET",
url: ajaxUrl,
datatype:"JSON",
success: function(jsonData){
    //Do something
},
error: function(jqXHR, textStatus){
    //handle connection errors
}});

This will work even if your json contains normal text.

suren179
  • 21
  • 1