0

I'm new to JavaScript, and trying to get a JSON object to post on my website. However, I can't get a success response. Since I don't have a proper debugger, I don't see error messages.

This is my code so far, I've read that it could be a security problem and I should look for JSONP, but I haven't found any proper examples to understand it.

<pre><html><head><title>Test0</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><body>
    <script>
        $.ajax({
        url: 'http://openraid.org/api/login/asd/asd',
        dataType: 'json',
        jsonpCallback: 'MyJSONPCallback', 
        success: function(data){
            alert("TEST");
        }
        });
    </script></head>
</body></html></pre>

So my question is, why don't I get a response?

Anticipating
  • 141
  • 1
  • 3
  • 13
  • 4
    If you're using a browser made in the past 5 years, you have a proper debugger. – Jason P Aug 20 '13 at 20:53
  • Answer: http://stackoverflow.com/questions/6849802/jquery-getjson-works-locally-but-not-cross-domain – Michael Walter Aug 20 '13 at 20:53
  • My jsonp call is successful but for som reason my "data" object is null even if it succed. Am I trying to attempt to read it incorrectly? I tried with alert(data.error); which should produce a readable text. – Anticipating Aug 20 '13 at 22:08

3 Answers3

0
$.getJSON("http://openraid.org/api/login/TE/ST",function(msg) {
    alert(msg.token);
})
.done(function(data, textStatus, jqXHR) { console.log( "second success" ); })
.fail(function(jqXHR, textStatus, errorThrown) { console.log( "error" ); });
hunter
  • 62,308
  • 19
  • 113
  • 113
0

I'd break this down to something like this:

$.ajax({
  type: "GET",
  dataType: "json",
  url: "http://openraid.org/api/login/TE/ST",  
})
.done(function(dataReturned){alert("Win")})
.fail(function(){alert("Fail")}));

Also, I recommend using the Firebug extension in Firefox for debugging purposes.

Ken Johnson
  • 330
  • 3
  • 5
0

very simply, JSONP is a server protocol that will take a callback parameter in the uri and returns a JSON object as a variable inside a javascript function call that is named by the callback parameter. So, in actual functionality, getJSON is working exactly like getScript, where it is able to load an external uri as a script tag into the page and run the script, in this case, the javascript function that is returning a variable containing the JSON. If your API does not support JSONP, then you will not get a success response.

Neil S
  • 2,304
  • 21
  • 28