最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Trying to use jquery ajax but can't parse JSON? - Stack Overflow

programmeradmin4浏览0评论

I'm trying to load some data via jQuery.getJSON() but it does not work:

here is my JSON:

{didwork=true,userid=123}

or it is

{didwork=false,userid=0}

here is my Javascript:

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

I'm trying to load some data via jQuery.getJSON() but it does not work:

here is my JSON:

{didwork=true,userid=123}

or it is

{didwork=false,userid=0}

here is my Javascript:

$.ajax({
  data["username"] = "u"
  data["password"] = "p";
  url: https://www.myurl./json.php,
  dataType: 'json',
  data: data,
  success: function(json){
    //fill it into div
  }
});
Share Improve this question asked Apr 28, 2012 at 15:30 wondrinobucherwondrinobucher 334 bronze badges 2
  • 3 If you're ever curious if your JSON is or is not right, I'd higly remend using JsonLint.. – Erik Philips Commented Apr 28, 2012 at 15:34
  • 1 That code is a syntax error; it cannot possibly work. You should always have your JavaScript console open. – Pointy Commented Apr 28, 2012 at 15:34
Add a ment  | 

2 Answers 2

Reset to default 7

your json string is wrong. it has to be

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

or

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

never use = and always use "

Your javascript is wrong..

you need to move the data initialization outside the ajax call..
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./json.php',
  dataType: 'json',
  data: data,
  success: function(json){
    //fill it into div
  }
});

Your json is wrong

should be {"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

发布评论

评论列表(0)

  1. 暂无评论