简体   繁体   English

试图使用jquery ajax但无法解析JSON?

[英]Trying to use jquery ajax but can't parse JSON?

I'm trying to load some data via jQuery.getJSON() but it does not work: 我正在尝试通过jQuery.getJSON()加载一些数据,但它不起作用:

here is my JSON: 这是我的JSON:

{didwork=true,userid=123}

or it is 或者是

{didwork=false,userid=0}

here is my Javascript: 这是我的Javascript:

$.ajax({
  data["username"] = "u"
  data["password"] = "p";
  url: https://www.myurl.com/json.php,
  dataType: 'json',
  data: data,
  success: function(json){
    //fill it into div
  }
});

your json string is wrong. 你的json字符串是错误的。 it has to be 它一定要是

{"didwork":true,"userid":123}

or 要么

{"didwork":false,"userid":0}

never use = and always use " 从不使用=并始终使用"

Your javascript is wrong.. 你的javascript错了..

you need to move the data initialization outside the ajax call.. 你需要在ajax调用之外移动data初始化..
plus the url needs to be quoted.. ( between ' ) 加上网址需要引用..( '之间

var data = {};
data["username"] = "u";
data["password"] = "p";

this could also be represented with 这也可以用

var data = {'username': 'u', 'password': 'p'};

and the call 和电话

$.ajax({
  url: 'https://www.myurl.com/json.php',
  dataType: 'json',
  data: data,
  success: function(json){
    //fill it into div
  }
});

Your json is wrong 你的json错了

should be {"didwork":true,"userid":123} 应该是{"didwork":true,"userid":123}


If the url is to a different site then the one making the call it will fail due to same origin policy 如果网址是到另一个网站,那么进行呼叫的网站将因相同的原始策略而失败

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

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