0

Ajax call is made successfully. And I see a JSON response. But some how the method fieldValidated is not getting called when the message return is a success.

Using Spring in the back

JSON response '{ "valid" : "false", "message" : "Some message" }'

   function thisMethodIsCalledOnCursorOutFromInputField() {
    $.ajax({
        url: '${pageContext. request. contextPath}/X.htm',
        data: {
            someId: $('#someId').val()
        },
        contentType: "*/*",
        dataType: "json", //Have also tried with "text"
        success: function (data) {
            console.log('response=', data);
            fieldValidated("someId", data);
        },
        error: function (data) {
            console.log('response=', data);
            fieldValidated("emailId",data);
        }
    });
    }

Console.log

Uncaught SyntaxError: Unexpected token o jquery-1.10.1.js:550
x.extend.parseJSON jquery-1.10.1.js:550 
$.ajax.error X.htm:115
c jquery-1.10.1.js:3074
p.fireWith jquery-1.10.1.js:3186 
k jquery-1.10.1.js:8255
r
java_dude
  • 3,631
  • 8
  • 30
  • 60
  • is it on the same domain? – reyaner Sep 03 '13 at 08:11
  • 1
    Have you checked the console for errors? – Rory McCrossan Sep 03 '13 at 08:13
  • hmm, you trying to get an html, did your try that as type? – reyaner Sep 03 '13 at 08:15
  • Are you sure `fieldValidated` is **not** being called? Or does it not execute properly? Maybe the error is in that function? – Tikkes Sep 03 '13 at 08:17
  • Yes its on same domain; I have updated the console log; HTTP status is `200` with the `JSON` string; Have not tried HTML type; `fieldValidated` is not getting called for `error` or `success` – java_dude Sep 03 '13 at 08:25
  • Are you getting some output to the console from `console.log('response=', data);` ? – Dmitry Igoshin Sep 03 '13 at 08:30
  • Getting nothing in the console.log. I just see the error message pasted above under `Console.log`. Though the HTTP response is success. Exception under `firebug` - SyntaxError: JSON.parse: unexpected character – java_dude Sep 03 '13 at 08:32
  • 2
    The error message kind of suggests that what your server is returning **isn't** valid JSON. Are those single quotes part of the text received from the server in response to the AJAX request? If so, remove them and try it again. – Anthony Grist Sep 03 '13 at 08:35
  • That was it. I guess. Had to remove the extra single quote. Makes me look so stu`. @Anthony can you post your answer so that I can up vote it. – java_dude Sep 03 '13 at 08:38
  • 1
    your server does not return json content type. read this post http://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type – AaA Sep 03 '13 at 08:39
  • @BobSort Thanks for providing the link. Very helpful. – java_dude Sep 03 '13 at 08:41

3 Answers3

2

The error message indicates that the response from the server isn't valid JSON. If the response is literally

'{ "valid" : "false", "message" : "Some message" }'

then you'll need to remove the single quotes, since they're not necessary to indicate to JavaScript that it's a string and aren't valid JSON (strings are contained within double quotes).

Anthony Grist
  • 37,428
  • 8
  • 62
  • 73
  • Actually he need to set content-type to `Application/JSON` too. – AaA Sep 03 '13 at 08:42
  • @BobSort He hasn't posted any server-side code, so I can't tell if he needs to or not (though jQuery's going to treat the text response as JSON regardless). If you're talking about the `contentType` property on the AJAX request, that refers to the data being sent, not received, and it doesn't look like he's sending JSON to me. – Anthony Grist Sep 03 '13 at 08:47
1

The error you see in console suggest that, jquery is expecting a mime type JSON.

You will need to set your content type to application/json

This post explains it.

On the other hand your JSON output need to be valid too,

you can check validity of your JSON output using JSONlint. you can download and run it from your local http server too

Community
  • 1
  • 1
AaA
  • 3,111
  • 8
  • 52
  • 77
0

If your JSON call return an array like yours, you have to specifically call for what you need.

fieldValidated("someId", data.message);

That might be your problem.

Omer
  • 164
  • 7