1

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

,,

grid
  • 13
  • 1
  • 3
  • 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 Aug 02 '11 at 05:02
  • because the page should be personalized based on url, so it can be shared in the desired state. – grid Aug 02 '11 at 05:47

3 Answers3

3

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);
    }
});
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • that works but is just hardcoding the variable in a different place. – grid Aug 02 '11 at 05:22
  • That was just an example. You can set any value that you need and append it to the url – ShankarSangoli Aug 02 '11 at 05:24
  • unfortunately when i comment out [or remove] var tsid = 'PIF1UFTOS10HF'; and append tsid=PIF1UFTOS10HF to the url instead, i get 'tsid is not defined'in the firebug JS console. – grid Aug 02 '11 at 05:41
  • That is because tsid is not available now since you commented it. What do you exactly need? – ShankarSangoli Aug 02 '11 at 05:46
  • i figured it out here: http://papermashup.com/read-url-get-variables-withjavascript/ - adding function getUrlVars() { var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { vars[key] = value; }); return vars; } var tsid = getUrlVars()["pp"]; where the url is appended with ?pp=PIF1UFTOS10HF – grid Aug 02 '11 at 05:54
0

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

Community
  • 1
  • 1
Jon P
  • 19,442
  • 8
  • 49
  • 72
0

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);
    }
});
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178