admin管理员组

文章数量:1414605

What does the &callback parameter in a URL do? The reason I am asking is because I am running a query with jquery's $.ajax function and when I include &callback=? in the end of the url I get a 400 (bad request) error.

Here is my javascript code:

var energy_query_array = [];


var masterEnergyTable_tableid = '145TOXaanydD63tvgVMmCVH0hei0NXNCEK8ekZBg';
var current_date = 'Jan-12';

var energy_query = 'SELECT * FROM ' + masterEnergyTable_tableid; 

var encoded_energy_query = encodeURIComponent(energy_query);
var energy_url = [''];
energy_url.push('?sql=' + encoded_energy_query);
energy_url.push('&key=' + apiKey); 
//energy_url.push('&callback=?');
$.ajax({
url: energy_url.join(''),
datatype: 'jsonp',
success: function(data) {
           var name_array = data['columns'];
           var data_array = data['rows'][0];
       for(var i = 1; i < data['columns'].length; i++) {
        energy_query_array[name_array[i]] = data_array[i];
       }
     }
});

console.log(energy_query_array);

I have mented out the line where I add &callback=? to the url because when I leave it in the query returns the error: "message":"Invalid value for parameter callback: ?"

However, earlier in the code I performed a similar query to google fusion tables using the same parameter for callback.

The console.log call at the end of the code displays an empty array to the console.

What does the &callback parameter in a URL do? The reason I am asking is because I am running a query with jquery's $.ajax function and when I include &callback=? in the end of the url I get a 400 (bad request) error.

Here is my javascript code:

var energy_query_array = [];


var masterEnergyTable_tableid = '145TOXaanydD63tvgVMmCVH0hei0NXNCEK8ekZBg';
var current_date = 'Jan-12';

var energy_query = 'SELECT * FROM ' + masterEnergyTable_tableid; 

var encoded_energy_query = encodeURIComponent(energy_query);
var energy_url = ['https://www.googleapis./fusiontables/v1/query'];
energy_url.push('?sql=' + encoded_energy_query);
energy_url.push('&key=' + apiKey); 
//energy_url.push('&callback=?');
$.ajax({
url: energy_url.join(''),
datatype: 'jsonp',
success: function(data) {
           var name_array = data['columns'];
           var data_array = data['rows'][0];
       for(var i = 1; i < data['columns'].length; i++) {
        energy_query_array[name_array[i]] = data_array[i];
       }
     }
});

console.log(energy_query_array);

I have mented out the line where I add &callback=? to the url because when I leave it in the query returns the error: "message":"Invalid value for parameter callback: ?"

However, earlier in the code I performed a similar query to google fusion tables using the same parameter for callback.

The console.log call at the end of the code displays an empty array to the console.

Share Improve this question edited Dec 6, 2012 at 20:34 azrosen92 asked Dec 6, 2012 at 14:21 azrosen92azrosen92 9,1374 gold badges28 silver badges47 bronze badges 3
  • 1 You are probably using JSONP and the server you are trying to connect does not support it. The callback parameter is necessary to make JSONP working. – freakish Commented Dec 6, 2012 at 14:22
  • What does the actual request URI look like? – BenM Commented Dec 6, 2012 at 14:24
  • Please show your javascript code – Explosion Pills Commented Dec 6, 2012 at 14:24
Add a ment  | 

3 Answers 3

Reset to default 3

Callback is used for JSONP requests which is mean't to be used when doing a cross domain request to fetch JSON data. Instead of using the XHR wrapper, it uses a script tag which then passes a callback querystring parameter which points to the function that is used to execute passing in all the JSON data inside. This way you can have a datapoint which has no idea of how your application works, but when you pass in a callback then it will fire a function with the data. This allows you to use it's API from any browser.

Like any other piece of data in a query string (or anywhere else between the hostname and the fragment id), it provides some data to the server. The meaning is entirely determined by the software running on that server.

There is a convention to use callback as the key for a value that will be used as the name of the function to call in a JSON-P response in a RESTful API. JSON-P is a means to provide a web service accessible across origins for browsers which do not support CORS.

e.g.

http://example./foo?callback=asdf

would give you

asdf([1,2,3]);

JSON-P, obviously, requires that the server support the sending of JSON-P responses. If you try to perform a cross-origin request with jQuery it will (by default) modify the request on the assumption that the server will support JSON-P with the usual conventions.

Read about Query Strings

They start with a ?, and additional parameters get concatnated with &. The ? should never be anywhere but the beginning of the string unless it's encoded.

The key value pairs then are available to the server, as this is how a standard GET request is performed.

For example:

http://myserver.?name=josh&age=24&state=il

Your web server is probably choking on the query string somewhere.

本文标签: javascriptURL ampcallback parameterStack Overflow