admin管理员组文章数量:1426929
I am trying to get a URL variable into a jQuery ajax function, in order to quickly adapt some code.
This should be simple, but i'm a bit of an idiot and using the methods described at .html didn't work out for me.
Essentially I am trying to replace the hard-coded "player_tsid" with a variable on the end of a url, like /?player_tsid=PIF575TP7IOBA
Here is the code ..
$(function(){
jQuery.support.cors = true;
$.ajax({
'url' : '.getAnimations',
'dataType' : 'json',
'data' : { 'player_tsid' : 'PIF1UFTOS10HF' },
'success' : function(data, textStatus, jqXHR){
if (data.ok){
g_sheets = data.sheets;
g_anims = data.anims;
build_index();
$('#loading').text("Loading sheets...");
load_sheets();
}else{
alert('api error');
}
},
'error' : function(jqXHR, textStatus, errorThrown){
alert('api error');
alert(errorThrown);
}
});
});
,,
I am trying to get a URL variable into a jQuery ajax function, in order to quickly adapt some code.
This should be simple, but i'm a bit of an idiot and using the methods described at http://jquery-howto.blogspot./2009/09/get-url-parameters-values-with-jquery.html didn't work out for me.
Essentially I am trying to replace the hard-coded "player_tsid" with a variable on the end of a url, like http://www.example./?player_tsid=PIF575TP7IOBA
Here is the code ..
$(function(){
jQuery.support.cors = true;
$.ajax({
'url' : 'http://api.glitch./simple/players.getAnimations',
'dataType' : 'json',
'data' : { 'player_tsid' : 'PIF1UFTOS10HF' },
'success' : function(data, textStatus, jqXHR){
if (data.ok){
g_sheets = data.sheets;
g_anims = data.anims;
build_index();
$('#loading').text("Loading sheets...");
load_sheets();
}else{
alert('api error');
}
},
'error' : function(jqXHR, textStatus, errorThrown){
alert('api error');
alert(errorThrown);
}
});
});
,,
Share Improve this question asked Aug 2, 2011 at 4:54 gridgrid 131 silver badge3 bronze badges 2- Why don't you put the desired player_tsid on there, server-side? as the player_tsid parameter is a get parameter, you can put it there at the server and make the javascript with the desired player_tsid embedded in it. – Saeed Commented Aug 2, 2011 at 5:02
- because the page should be personalized based on url, so it can be shared in the desired state. – grid Commented Aug 2, 2011 at 5:47
3 Answers
Reset to default 3Just do this
var tsid = 'PIF1UFTOS10HF';
$.ajax({
'url' : 'http://api.glitch./simple/players.getAnimations?player_tsid='+tsid,
'dataType' : 'json',
'success' : function(data, textStatus, jqXHR){
if (data.ok){
g_sheets = data.sheets;
g_anims = data.anims;
build_index();
$('#loading').text("Loading sheets...");
load_sheets();
}else{
alert('api error');
}
},
'error' : function(jqXHR, textStatus, errorThrown){
alert('api error');
alert(errorThrown);
}
});
Or, you could use this -
var tsid = 'PIF1UFTOS10HF';
$.ajax(
{
'type': "GET",
'url': "http://api.glitch./simple/players.getAnimations",
'dataType': "json",
'data' : { 'player_tsid' : tsid },
'success' : function(data, textStatus, jqXHR)
{
if (data.ok)
{
g_sheets = data.sheets;
g_anims = data.anims;
build_index();
$('#loading').text("Loading sheets...");
load_sheets();
}
else
{
alert('api error');
}
},
'error' : function(jqXHR, textStatus, errorThrown)
{
alert('api error');
alert(errorThrown);
}
});
An SO Answer for you: How can I get query string values in JavaScript?
Basically:
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
$(function(){
jQuery.support.cors = true;
$.ajax({
'url' : 'http://api.glitch./simple/players.getAnimations',
'dataType' : 'json',
'data' : { 'player_tsid' : getParameterByName("player_tsid") },
'success' : function(data, textStatus, jqXHR){
if (data.ok){
g_sheets = data.sheets;
g_anims = data.anims;
build_index();
$('#loading').text("Loading sheets...");
load_sheets();
}else{
alert('api error');
}
},
'error' : function(jqXHR, textStatus, errorThrown){
alert('api error');
alert(errorThrown);
}
});
})
I also like this answer to the same question
本文标签: javascriptHow do you use a URL variable inside a jQuery ajax functionStack Overflow
版权声明:本文标题:javascript - How do you use a URL variable inside a jQuery ajax function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745470610a2659730.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论