开发者

Ajax URL property with API variable

All I want to do is to be able to specify a User ID and API Key in the URL of an Ajax call:

Currently doing:

$.ajax({
  url: "https://www.googleapis.com/plus/v1/people/115332174513505898112?key=AIzaFrCzbKLawSPG8C0tjZDozO1bSWx49mJm13s",
  context: document.body,
  dataType: 'jsonp',
    success: function(data){
      //blah...
    }
});
开发者_JAVA技巧

However I want to be able to set the UserID and API Key dynamically so want there values in the URL: to be dynamic:

E.g.

var userId = 115332174513505898112;
var apiKey = AIzaSyCzbKLawSPG8C0tjZDozO1bSWx49mJm13s;

url: "https://www.googleapis.com/plus/v1/people/" + userId + "?key=" + apiKey,

However, as soon as a define the variables inside the $.ajax({ function the JavaScript stops working and I get an error of:

missing : after property id
[Break On This Error] var userId = 115332174513505898112; 

What am I doing wrong, I'm sure I've done this before?


This should do it:

var userId = "115332174513505898112";
var apiKey = "AIzaSyCzbKLawSPG8C0tjZDozO1bSWx49mJm13s";
$.ajax({
  url: "https://www.googleapis.com/plus/v1/people/" + userId + "?key=" + apiKey,
  context: document.body,
  dataType: 'jsonp',
    success: function(data){
      //blah...
    }
});

Items to note:

  • Quotes around the values of the userId and apiKey. (You may not need them around the userId value; you absolutely do around the apiKey.)
  • You define those outside of the actual ajax function call.

I'm assuming the code is already inside a function, e.g:

function foo() {
    var userId = "115332174513505898112";
    var apiKey = "AIzaSyCzbKLawSPG8C0tjZDozO1bSWx49mJm13s";
    $.ajax({
      url: "https://www.googleapis.com/plus/v1/people/" + userId + "?key=" + apiKey,
      context: document.body,
      dataType: 'jsonp',
        success: function(data){
          //blah...
        }
    });
}

If not, put it in one to avoid creating global variables with those var statements.


You should put that values of your variables in quotes...

Additionally, you can not write any javascript code within objects - you need to declare your variables outside the object, and refer to them inside - like this:

var userId = "115332174513505898112";
var apiKey = "AIzaSyCzbKLawSPG8C0tjZDozO1bSWx49mJm13s";

$.ajax({
  url: "https://www.googleapis.com/plus/v1/people/" + userId + "?key=" + apiKey,
  context: document.body,
  dataType: 'jsonp',
    success: function(data){
      //blah...
    }
});


Use this code. Strings have to be contained within " (quotation marks). Also, you cannot define variables using var within { ... } (object, not to be confused with markers of a function/loop/condition body).

var userId = "115332174513505898112";
var apiKey = "AIzaSyCzbKLawSPG8C0tjZDozO1bSWx49mJm13s";


$.ajax({
  url: "https://www.googleapis.com/plus/v1/people/"+userId+"?key="+api,
  context: document.body,
  dataType: 'jsonp',
    success: function(data){
      //blah...
    }
});


You have to define the variables (the var statements) outside the object {} and the function call $.ajax().

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜