Spyros_av
Spyros_av

Reputation: 893

How to pass Javascript variables inside a URL? AJAX

i am trying to pass the values of accesstoken and pageid inside the url that i use. Any ideas how to do it correctly?

<script type="text/javascript">   
function makeUrl() {
    var accesstoken = "12345679|bababashahahhahauauuaua";
    var pageid =  "<?php echo $page_id;?>";
 $.ajax(
  {
    url: 'https://graph.facebook.com/?pageid/?access_token='+pageid+accesstoken,
 statusCode: {......

Upvotes: 7

Views: 35917

Answers (4)

Matt
Matt

Reputation: 4752

You can also use the "data" setting. This will convert it to a query string.

<script type="text/javascript">   
function makeUrl() {
    var accesstoken = "12345679|bababashahahhahauauuaua";
    var pageid =  "<?php echo $page_id;?>";
 $.ajax(
  {
    url: 'https://graph.facebook.com/',
    data: 'pageid='+pageid+'&access_token='+accesstoken
 statusCode: {......

Upvotes: 2

Imran
Imran

Reputation: 4750

Change

url: 'https://graph.facebook.com/?pageid/?access_token='+pageid+accesstoken,

to

url: 'https://graph.facebook.com/?pageid='+pageid+'&access_token='+accesstoken,

Upvotes: 10

KDP
KDP

Reputation: 1481

 'https://graph.facebook.com/?pageid='+pageid+'&access_token='+accesstoken

Upvotes: 1

lucasfcosta
lucasfcosta

Reputation: 295

You can create your url using:

function makeUrl() {
  var accesstoken = '12345679|bababashahahhahauauuaua';
  var pageid =  'example'
  return 'https://graph.facebook.com/?pageid=' + pageid + '&access_token=' + accesstoken;
}

And then pass it to your AJAX function.

function doAjax(_url) {
  return $.ajax({
    url: _url,
    type: 'GET'
  });
}

doAjax(makeUrl());

And now an elegant way to handle your success callback:

doAjax(makeUrl()).success(function() {
  // this will be executed after your successful ajax request
});

Upvotes: 0

Related Questions