Question

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.

Was it helpful?

Solution

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)

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top