0

I am totally new to JSONP, AJAX and JQuery. I am trying to retrieve some data from a given URL, but it is not getting to my alert("Success!") after the getJSON. I'm struggling to work out why, or what to do next. Any advice?

(Note, I have replaced the actual URL for xx in this question)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <html xmlns=" http://www.w3.org/1999/xhtml "> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Request json test</title> <script src="http://code.jquery.com/jquery-1.5.js"></script> <script src="json-jquery.js" type="text/javascript"></script> <script src="json-jquery.js" type="text/javascript"></script> <a href="json-data.php"></a> <script> $(document).ready(function(){ $.getJSON('http://xx.xxx.xxx.xx/getCourses.php?action=getUnpaid', function( data ) { alert("Success!"); }); }); </script> </head> <body> <a href="#" id="getdata-button">Get JSON Data</a> <div id="showdata"></div> </body> </html> 
16
  • is that URL there on the same domain as the HTML document? Commented Jul 26, 2014 at 13:42
  • It is an external URL to me. Does that answer your question? Commented Jul 26, 2014 at 13:43
  • Then this is your "problem": developer.mozilla.org/en-US/docs/Web/Security/… Commented Jul 26, 2014 at 13:45
  • do you set allow access to this external file getCourses.php Commented Jul 26, 2014 at 13:45
  • @Sirko Ok, thanks. So how do I edit my code to be able to access this file, or what do I read to work it out? This is the task I have at hand to learn this stuff. Commented Jul 26, 2014 at 13:47

1 Answer 1

2

The getJSON method doesn't do a JSONP request. To use JSONP you can use the ajax method, so that you can specify the data type:

$(document).ready(function(){ $.ajax({ url: 'http://xx.xxx.xxx.xx/getCourses.php?action=getUnpaid', dataType: 'jsonp', success: function( data ) { alert("Success!"); } }); }); 

A JSON request is made using the XMLHTTPRequest object, and is subject to the same origin policy. JSONP was introduced to circumvent this, and uses a script tag to load the resource. As a script can be loaded from a different domain, it's not subject to the same origin policy.

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

10 Comments

Its probably my fault as I am totally new to this, but this block of code breaks my page. I dont know how to even find the error message tho. Im using NetBeans.
@AndyA: You should open the error console and look for error messages. In Internet Explorer press F12 and select the second tab. In Firefox in the menu: Tools > Web Developer > Web Console. In Chrome press F12 and select the Console tab.
Thanks I'll do that now. Meanwhile, can you please double check all your brackets are opened and closed correctly? It looks a little bizarre to me.
It says SyntaxError: missing } after property list. Is this from your code snippet? Or possibly I pasted it in wrong.
@AndyA: Check against how the code looks now. I added a } right after posting the answer, you might have copied it before that.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.