0

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>
SAWJUSTO
  • 369
  • 1
  • 5
  • 19

2 Answers2

1

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.

Ankit Jaiswal
  • 22,859
  • 5
  • 41
  • 64
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

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

Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • I try as Rohan Kumar said, i got this error in console " SyntaxError: invalid label {"status":1,"movies":[{"id":1,"name":"Irma Crona","cinemas":[" – SAWJUSTO Jun 24 '13 at 05:39
  • Can you change the `key name` **status** to **mystatus** on http://mubi-app.herokuapp.com/api/v1/home page, if yes then do that and check. – Rohan Kumar Jun 24 '13 at 05:47