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
Add a ment  | 

3 Answers 3

Reset to default 3

Just 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