简体   繁体   中英

jQuery ajax unable retrieve json

So here is the ajax call that is making the problem

$.ajax({
    type: "POST",
    url: "src/login.php",
    dataType: "JSON",
    data: {username: usr, password: pwd},
    success: function(json){
        loggedStatus=json.status;
        alert(json.status);
    }
});

It is succesfully passing the variables to the php file, but it isn't entering th success: part. This is example of what the php file returns

{
    "status": "Wrong"
}

or

{
    "status": "154414707fe8d22bb6239648ce11a9c9bede1a3e"
}

Which is totaly fine.

Also, try typing json instead of JSON in the dataType. Avoid using success . From the jQuery website :

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

A bit surprised to see this example on the jQuery ajax page :

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
  alert( "Data Saved: " + msg );
});

Try putting the url as the first parameter in your $.ajax({ function.

because the syntax is: $.ajax(url[, options])

Remove the dataType

$.ajax({
    type: "POST",
    url: "src/login.php",
    data: {username: usr, password: pwd},
    success: function(result){
        var json = jQuery.parseJSON(result);
        alert(json.status);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM