jQuery how to build a link with json?, error
i have this:
$.get('xxx.php', { username: userName },
function(data){
var get_back = data;
alert(get_back);
});
this will return get_back=12345
and i'm trying to build this:
url: "http://www.test.com/users/" + get_back,
the result to be http://www.test.com/users/12345
for some reason it doesn't want to work. If i hard-code the 12345
in the link it will work. i've also tryed url: "http://www.test.com/users/" + get_back + "",
and url: 'h开发者_StackOverflow社区ttp://www.test.com/users/' + get_back,
any ideas?
edit:
$.ajax({
type: "POST",
data: JSON.stringify(formData),
dataType: "json",
url: "http://www.test.com/users/" + get_back + "",
success: function(t){ alert(t); }
});
that because get_back
might be in the scope of everything else. either you can call get_back
before the $.get
call and it will be global or you can put it all into on object like this:
var get = {
get_back: null,
init: function(username){
var self = this;
$.get('xxx.php', { username: userName },
function(data){
self.get_back = data;
self.runAjax(); //run the ajax when get_back is instantiated
}
});
},
runAjax: function(){
var self = this;
$.ajax({
type: "POST",
data: JSON.stringify(formData),
dataType: "json",
url: "http://www.test.com/users/" + self.get_back + "",
success: function(t){ alert(t); }
});
}
}
//to use it:
get.init(username);
//to use get_back
alert(get.get_back);
UPDATED WITH AJAX
you might have to add formdata
to the object somehow, i dont know where that was created
精彩评论