简体   繁体   English

jQuery Ajax无法检索json

[英]jQuery ajax unable retrieve json

So here is the ajax call that is making the problem 所以这是导致问题的ajax调用

$.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. 它已成功将变量传递到php文件,但未进入成功:部分。 This is example of what the php file returns 这是php文件返回的示例

{
    "status": "Wrong"
}

or 要么

{
    "status": "154414707fe8d22bb6239648ce11a9c9bede1a3e"
}

Which is totaly fine. 完全可以。

Also, try typing json instead of JSON in the dataType. 另外,尝试在dataType中键入json而不是JSON Avoid using success . 避免使用success From the jQuery website : 从jQuery网站:

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. 弃用通知:从jQuery 1.8开始,不再使用jqXHR.success(),jqXHR.error()和jqXHR.complete()回调。 To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead. 要准备将其最终删除的代码,请改用jqXHR.done(),jqXHR.fail()和jqXHR.always()。

A bit surprised to see this example on the jQuery ajax page : 看到这个示例在jQuery ajax页面上有点惊讶:

$.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. 尝试将url作为$ .ajax({函数中的第一个参数。

because the syntax is: $.ajax(url[, options]) 因为语法是: $.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);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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