I have token value in a URL like .php?action=token
I need to consume this URL data which is a random string, I'm trying with following code:
var jqxhr = $.get(".php?action=token", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
// Perform other work here ...
// Set another pletion function for the request above
jqxhr.always(function() {
alert( "second finished" );
});
Using $.get()
While trying to load, it's showing an error. I'm just stuck with it, how to get the data?
I have token value in a URL like http://example./api.php?action=token
I need to consume this URL data which is a random string, I'm trying with following code:
var jqxhr = $.get("http://example./api.php?action=token", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
// Perform other work here ...
// Set another pletion function for the request above
jqxhr.always(function() {
alert( "second finished" );
});
Using $.get()
While trying to load, it's showing an error. I'm just stuck with it, how to get the data?
Share Improve this question edited Jan 22, 2014 at 13:05 MackieeE 11.9k4 gold badges41 silver badges58 bronze badges asked Jan 22, 2014 at 13:00 sukantasukanta 5416 silver badges18 bronze badges 11- what error are you getting? – taxicala Commented Jan 22, 2014 at 13:01
- What kind of data do you want to request? Is it Json? – Rob Commented Jan 22, 2014 at 13:04
- now getting "error" as in alert and GET status is failed – sukanta Commented Jan 22, 2014 at 13:05
- Is the service on your own domain? If not, you'll need to do either a CORS or JSONP request, and both of those need to be enabled on the server. – Andy Commented Jan 22, 2014 at 13:05
-
to access data you need to add a parameter on your callbacks e.g.
.done(function(data) {
– melc Commented Jan 22, 2014 at 13:06
3 Answers
Reset to default 4Cross domain calls are restricted by the browser so some solutions are,
1.Cross-Origin Resource Sharing (CORS) or jsonp, but you will require to have access to the server you are calling and configure that (many examples online e.g. How to make cross domain request)
2.server side proxy - create simple server side code e.g. php page that you will call form js and place code in php that calls the targeted cross domain server and return the results to your js. e.g. AJAX cross domain call
try this
$.ajax({
url:"http://example./api.php?action=token",
type: "GET",
success: function (data) {
alert(data)
}
});
try this
$.ajax({
url:"http://example./api.php?action=token",
type: "POST", //try this
success: function (data) {
alert(data)
}
});