4

I have token value in a URL like http://example.com/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.com/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 completion 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?

11
  • what error are you getting? Commented Jan 22, 2014 at 13:01
  • What kind of data do you want to request? Is it Json? Commented Jan 22, 2014 at 13:04
  • now getting "error" as in alert and GET status is failed 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. Commented Jan 22, 2014 at 13:05
  • to access data you need to add a parameter on your callbacks e.g. .done(function(data) { Commented Jan 22, 2014 at 13:06

3 Answers 3

4

Cross 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

Sign up to request clarification or add additional context in comments.

Comments

0

try this

  $.ajax({
              url:"http://example.com/api.php?action=token",
              type: "GET",
              success: function (data) {
                   alert(data)
              }
           });

4 Comments

this might be a cross domain issue
for cross domain you should use the jsonp
for cross domain you check my below post stackoverflow.com/questions/21282846/…
so if i dont get the data in json format i cant receive this in clinet end with this
0

try this

  $.ajax({
              url:"http://example.com/api.php?action=token",
              type: "POST",           //try this
              success: function (data) {
                   alert(data)
              }
           });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.