java_dude
java_dude

Reputation: 4088

JQuery Ajax success is not getting executed

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

Upvotes: 0

Views: 1039

Answers (3)

AaA
AaA

Reputation: 3694

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

Upvotes: 1

Anthony Grist
Anthony Grist

Reputation: 38345

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

Upvotes: 2

Omer
Omer

Reputation: 164

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.

Upvotes: 0

Related Questions