Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

This question already has an answer here:

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?

share|improve this question

marked as duplicate by tkone, Paul, Tala, RGraham, Michael Härtl Aug 21 '13 at 6:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
cross domain issue. – zsong Aug 20 '13 at 20:52
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: stackoverflow.com/questions/6849802/… – 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 Answers 3

$.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" ); });
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
    
I get error on dataType: "json". "SyntaxError: missing } after property list [Stanna vid fel] dataType: "json", Is my error code – Anticipating Aug 20 '13 at 21:52
    
i'm sorry about that, missed a comma after "GET". – Ken Johnson Sep 9 '13 at 13:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.