开发者

How do you use a URL variable inside a jQuery ajax function?

I am trying to get a URL variable into a jQuery ajax function, in order to quickly adapt some code.

开发者_如何学Go

This should be simple, but i'm a bit of an idiot and using the methods described at http://jquery-howto.blogspot.com/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.com/?player_tsid=PIF575TP7IOBA

Here is the code ..

$(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);
    }
});
});

,,


Just do this

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);
    }
});


Or, you could use this -

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);
    }
});


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.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);
    }
});
})

I also like this answer to the same question

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜