SAWJUSTO
SAWJUSTO

Reputation: 369

I can't retrieve data from my api link with ajax

Before i retrieve data from my link (http://mubi-app.herokuapp.com/api/v1/home), i test it with browser, it is ok. it returns Json format, but i got error when i access that link with ajax. Thanks,

Here is my code .....

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
  $.ajax({
    type:"GET"
    , url:"http://mubi-app.herokuapp.com/api/v1/home"
    , dataType:"jsonp"
    , success: function(data){
        debugger;

        $('#result').text(data)

    } 
   , error: function(e) {
       debugger;
       console.log(e)
       alert(e + "Error");
       }
    });
});

</script>

Upvotes: 0

Views: 2688

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

Since it is a cross-domain request you are trying to use jsonp request, but for that to work the server needs to support it. In this case it does not seem to be the case.

So I don't think it is possible to create a browser side call to the said API.

One possible solution is to write a server side wrapper for the said API, can in server side delegate the request to the said API.

Upvotes: 1

Rohan Kumar
Rohan Kumar

Reputation: 40639

You should create a jsonpcallback function to solve your issue, like

$(document).ready(function() {
  $.ajax({
    type:"GET"
    , url:"http://mubi-app.herokuapp.com/api/v1/home"
    , dataType:"jsonp",
    jsonpCallback: "localJsonpCallback",
     success: function(data){
        debugger;

        $('#result').text(data)

    } 
   , error: function(e) {
       debugger;
       console.log(e)
       alert(e + "Error");
       }
    });
});

function localJsonpCallback(json) {
    console.log(json);
}

Read http://api.jquery.com/jquery.ajax/

See this

Upvotes: 0

Related Questions