Reputation: 1
I have the following jQuery AJAX request:
function addRecord() {
console.log('addRecord');
$.ajax({
type: 'POST',
url: rootURL,
contentType: 'application/json',
dataType: "json",
data: formToJSON(),
success: function(data, textStatus, jqXHR){
alert('form submitted successfully');
alert('data'+data);
},
error: function (xhRequest, ErrorText, thrownError) {
alert("Failed to process request correctly, please try again");
console.log('xhRequest: ' + xhRequest + "\n");
console.log('ErrorText: ' + ErrorText + "\n");
console.log('thrownError: ' + thrownError + "\n");
}
});
}
function formToJSON() {
return JSON.stringify({"dateofVisit": $('#dateofVisit').val()});
}
Following is the output that i receive in firebug.
Server Apache-Coyote/1.1
Access-Control-Allow-Orig... *
Access-Control-Allow-Cred... true
Access-Control-Allow-Meth... GET, POST, DELETE, PUT, OPTIONS, HEAD
Access-Control-Allow-Head... Content-Type, Accept, X-Requested-With
Content-Type text/plain
Transfer-Encoding chunked
Date Thu, 24 Oct 2013 09:37:34 GMT
Connection close
Request Headersview source
Host localhost:8080
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10
Accept application/json, text/javascript, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Content-Type application/json; charset=UTF-8
Referer http://mydomain.com/DemoPurpose/demo.html
Content-Length 28
Origin http://mydomain.com
Pragma no-cache
Cache-Control no-cache
JSON
dateofVisit
"23-10-2013"
Source
{"dateofVisit":"23-10-2013"}
empty
xhRequest: [object Object]
ErrorText: error
thrownError:
I am new to jquery, so basically dont know where i am doing the mistake. one thing is sure that its going to error part of ajax call but why? its able to form json...then why its going to error part? Please help me.
Upvotes: 0
Views: 7397
Reputation: 1188
from jquery doc
when you put dataType=json
, it means you should not return the empty content.
"json": Evaluates the response as JSON and returns a JavaScript object. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)
And also, you need check your response status is 200 not 400.
Your server side code has some problem.
@POST @Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.TEXT_PLAIN}) @Path("/hello")
//---------------------^^^ why you use plain text? why not application json
public Response create(String name) throws JSONException
{
System.out.println("creating record for account");
//JSONObject jsonObj = new JSONObject(name);
//------------------------------^^^^^ it's not jsonformat, error should be here
//try this way
JSONObject jsonObj = new JSONObject();
jsonObj.append("name",name);
return Response.status(201).entity(jsonObj).build();
}
Upvotes: 2