Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: JSONP Error

  1. #1
    New Coder
    Join Date
    Aug 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    JSONP Error

    I'm getting an error when attempting to get some JSONP data. Here is the error:
    Uncaught SyntaxError: Unexpected token :

    My function looks like this:
    Code:
    var urlShopify = 'https://XXxxxX@xxxx.myshopify.com/admin/orders.json?callback=?';
    $.getJSON(urlShopify).done(function(x){console.log(x.order_number)});
    This is the first time I've done anything with JSONP or even JSON at all. I'm not sure what I'm doing wrong.

    I changed some of the data to ensure security.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    What does the JSONP result look like? Maybe there is a syntax error in the JSON part of the response.

  • #3
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    you are not specifying any data to be appended to the url

  • #4
    New Coder
    Join Date
    Aug 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    What does the JSONP result look like? Maybe there is a syntax error in the JSON part of the response.
    I've taken the response and loaded it into JSONlnt and it validates perfectly. So I don't know if that is it.

    @DanInMa: How do I append to the URL? (forgive me this is the first time I've encountered JSON or JSONP)

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    A JSONP response should NOT validate perfectly on JSONlint. Did you strip off the function call from the JSONP before you validated it?

  • #6
    New Coder
    Join Date
    Aug 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    No I didn't remove anything from it.

    On of the site admin's just told me that I need to use a proxy and I have been looking into creating a proxy but am a bit lost on that one.

  • #7
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    There are a few slight but yet important differences between JSON and JSONP

    - Ajax requests returning JSONP will also work on external servers, whereas ajax requests returning JSON will only work if the external server explicitly allows it (by sending the Access-Control-Allow-Origin response header)
    - JSONP consists of a JSON string wrapped in a Javascript function call. It looks like
    Code:
    javascriptFunction(JSONgoeshere);
    
    // or often
    ?(JSONgoeshere);
    The name of the javascript function is provided to the JSONP call by the callback=xxxxx parameter. In return, the calling javascript will execute this function after reception (or replace the '?' function by the respective ajax callback and calling it)
    - JSON itself is a string containing the literal representation of a javascript object. Something like
    Code:
    {"key1": "value1", "key2" : "value2"}
    EDIT: And last but not least: A (server side) proxy is a script that will relay the call to an external site (returning JSON) via the local server. This will allow the Javascript client to request JSON indirectly from an external server and avoid the Same Origin Policy. The proxy script will do nothing more than forward the incoming request identically to the external server and output its result to the calling Javascript client.

    EDIT2: This is an example for a simple PHP proxy. Just attach the destination URL als a parameter to this script (something like proxy.php?url=.......). Don't forget to make it URI compliant by using encodeURIComponent(URLtoExternalScript): http://snipplr.com/view/62368/simple-php-proxy/
    Last edited by devnull69; 08-29-2013 at 06:23 PM.

  • #8
    New Coder
    Join Date
    Aug 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I am using a classic ASP Proxy which returns all of the data. I am saving all of that data in a local .js file so that I can load it later. My problem now need to move to another forum I believe.

  • #9
    New Coder
    Join Date
    Aug 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I also wanted to thank you guys for all of the help. It has been great.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •