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

I am trying to load some data via jQuery.getJSON (), but this does not work:

here is my json:

{didwork=true,userid=123}

or that

{didwork=false,userid=0}

here is my 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
  }
});
+3
source share
2 answers

Your json string is incorrect. he should be

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

or

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

never use =and always use"

+7
source

Your javascript is wrong.

you need to move initialization dataoutside of ajax call.
plus URL must be specified .. (between ')

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

this can also be represented using

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

and challenge

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

Your json is wrong

it should be {"didwork":true,"userid":123}


URL- , , , -

+4

All Articles