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

时间:2022-10-07 18:06:53

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
  }
});

2 个解决方案

#1


7  

your json string is wrong. it has to be

你的json字符串是错误的。它一定要是

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

or

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

never use = and always use "

从不使用=并始终使用“

#2


4  

Your javascript is wrong..

你的javascript错了..

you need to move the data initialization outside the ajax call..
plus the url needs to be quoted.. (between ')

你需要在ajax调用之外移动数据初始化..加上url需要引用..(在'之间)

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

如果网址是到另一个网站,那么进行呼叫的网站将因相同的原始策略而失败

#1


7  

your json string is wrong. it has to be

你的json字符串是错误的。它一定要是

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

or

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

never use = and always use "

从不使用=并始终使用“

#2


4  

Your javascript is wrong..

你的javascript错了..

you need to move the data initialization outside the ajax call..
plus the url needs to be quoted.. (between ')

你需要在ajax调用之外移动数据初始化..加上url需要引用..(在'之间)

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

如果网址是到另一个网站,那么进行呼叫的网站将因相同的原始策略而失败