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 3 of 3

Thread: JSONP vs. XHR

  1. #1
    New Coder
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    8
    Thanked 1 Time in 1 Post

    JSONP vs. XHR

    I was wondering if someone could clarify for me the cases you would use XML HTTP Request over a JSONP. From my understanding, they both can retrieve files but the XHR has the same origin policy which JSONP doesn't. It also appears that JSONP is much simpiler to implement over XHR as it only requires a script tag while XHR has a series of tags. So...Maybe i just don't understand the XHR or the JSONP. Could someone clarify these 3 questions for me:
    1) The purpose of XHR and JSONP.
    2) The use cases for XHR and JSONP
    3) When you program, how often/when do you personally tend to use XHR over JSONP.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Quote Originally Posted by and0rsk View Post
    I was wondering if someone could clarify for me the cases you would use XML HTTP Request over a JSONP. From my understanding, they both can retrieve files but the XHR has the same origin policy which JSONP doesn't. It also appears that JSONP is much simpiler to implement over XHR as it only requires a script tag while XHR has a series of tags. So...Maybe i just don't understand the XHR or the JSONP. Could someone clarify these 3 questions for me:
    1) The purpose of XHR and JSONP.
    2) The use cases for XHR and JSONP
    3) When you program, how often/when do you personally tend to use XHR over JSONP.
    Both provide a way to retrieve information from the server.

    Both XHR2 and JSONP can retrieve from other domains - only the origiinal XHR has a same domain policy.

    JSONP can only retrieve data in one specifiic format. XHR can retrieve in any format including XML and JSON and even plain text.

    XHR can retrieve static XML files without requiring any server side processing.

    XHR triggers an even when the data is returned and can then perform error handling - JSONP simply runs if it fis successfully retrieved and does nothing if the request fails.

    XHR involves simpler code with better error handling than JSONP - compare http://javascriptexample.net/ajax03.php to http://javascriptexample.net/ajax07.php

    Neither is the simplest way to call the server when you want to send data and don't need a response back - see http://javascriptexample.net/ajax08.php
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by felgall View Post
    Both provide a way to retrieve information from the server.

    Both XHR2 and JSONP can retrieve from other domains - only the origiinal XHR has a same domain policy.

    JSONP can only retrieve data in one specifiic format. XHR can retrieve in any format including XML and JSON and even plain text.

    XHR can retrieve static XML files without requiring any server side processing.

    XHR triggers an even when the data is returned and can then perform error handling - JSONP simply runs if it fis successfully retrieved and does nothing if the request fails.

    XHR involves simpler code with better error handling than JSONP - compare http://javascriptexample.net/ajax03.php to http://javascriptexample.net/ajax07.php

    Neither is the simplest way to call the server when you want to send data and don't need a response back - see http://javascriptexample.net/ajax08.php


    Very well said. With this,I'm sure you'll know what to choose between the two. If you are to choose one!


  •  

    Tags for this Thread

    Posting Permissions

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