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
2 Answers
Reset to default 7your 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