我正在尝试将一个URL变量放入jQuery ajax函数中,以便快速调整一些代码。
这应该很简单,但我有点白痴,使用http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html中描述的方法对我来说没有用。
基本上我试图用网址末尾的变量替换硬编码的“player_tsid”,例如http://www.example.com/?player_tsid=PIF575TP7IOBA
这是代码..
$(function(){
jQuery.support.cors = true;
$.ajax({
'url' : 'http://api.glitch.com/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);
}
});
});
,,
答案 0 :(得分:3)
就这样做
var tsid = 'PIF1UFTOS10HF';
$.ajax({
'url' : 'http://api.glitch.com/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);
}
});
答案 1 :(得分:0)
SO答案: How can I get query string values in JavaScript?
基本上:
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.com/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);
}
});
})
我也喜欢this answer同样的问题
答案 2 :(得分:0)
或者,您可以使用此 -
var tsid = 'PIF1UFTOS10HF';
$.ajax(
{
'type': "GET",
'url': "http://api.glitch.com/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);
}
});