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
11
  • is it on the same domain? Commented Sep 3, 2013 at 8:11
  • 1
    Have you checked the console for errors? Commented Sep 3, 2013 at 8:13
  • hmm, you trying to get an html, did your try that as type? Commented Sep 3, 2013 at 8:15
  • 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. Commented Sep 3, 2013 at 8:35
  • 1
    your server does not return json content type. read this post stackoverflow.com/questions/477816/… Commented Sep 3, 2013 at 8:39

3 Answers 3

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Actually he need to set content-type to Application/JSON too.
@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.
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

Comments

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.

3 Comments

There should not be a problem passing an array to a function.
It's not an array, anyway, it's an object. Both should be fine to pass to a function though.
I handle the JSON in the method call fieldValidated as you say but its not even getting called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.