InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

jQuery tutorials

    jQuery Ajax

    • jQuery JSONP
    • jQuery .post()
    • jQuery .load()
    • load script with cache enabled
    • jQuery .ajax() file upload

    jQuery CSS

    • change table cells color based on value
    • check if element is visible in viewport
    • display auto fade out message
    • fitvids.js for automatically resizing videos
    • get class list of an element
    • jQuery toggleClass() examples
    • make a div stick at top on scroll
    • table with alternate color rows

    jQuery Events

    • Javascript/jQuery - disable right click
    • jQuery - text input field - change keyup and paste events
    • trigger click using jQuery or Javascript

    jQuery DOM

    • jQuery html() vs text()
    • Check if an element exists
    • find total DOM elements
    • jQuery encode string to html entities
    • jQuery - read and modify iframe content
    • jQuery - create iframe with content
    • jQuery - find select element selectedIndex, value and text
    • jQuery - get checkbox value and checked state
    • jQuery - get javascript object

    jQuery Cookbook

    • get jQuery version
    • jQuery - add text to existing div
    • jQuery sliding effect - slideUp, slideDown, slideToggle

    jQuery UI

    • jQuery UI make element movable
    • jQuery ui slider and input text box - one way binding
    • jQuery ui slider and input text box - two way binding
     
    • Home
    • > Tutorials
    • > Javascript
    • > jQuery

    jQuery jsonp and cross domain ajax

    By admin | Last updated on Jun 6, 2020

    JSONP or “JSON with padding” provides a method to request data from a server having a different domain. Typical web browser prohibit cross domain request due to same origin policy.
    jquery-jsonp

    Jsonp example

    Here is a simple example of jsonp which fetches json data from a different domain:

    <html>
    <head>
    <title>JSONP example</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.js"></script>
    </head>
    <body>
    <h1>JSONP example</h1>
    Following data is from cross domain server:
    <pre style="background-color:lightgray;" id="jsondata"></pre>
    </body>
    </html>
    <script>
    $.getJSON('https://www.infoheap.com/api/jsonp.php?jsonp=?', function(data) {
      $("#jsondata").text(JSON.stringify(data));
      console.log(data);
    });
    </script>
    refresh done
    try it online

    Here is the server side php code which serves json data:

    <?php
    $function_name = $_GET['jsonp'];
    
    $json_array = array();
    $json_array['key1'] = "value1";
    $json_array['key2'] = "value2";
    
    echo "$function_name (\n";
    print json_encode($json_array);
    echo ");\n";
    ?>
    
    Note that the parameter “jsonp” in request and the server side code is same. We can use any other name but it should be same on client and server side. Also note that the question mark (?) in jsonp=? is filled by a unique id (different for each request).

    Jsonp and cache

    Jquery requests are not cached. The mechanism uses different jsonp value and hence the callback function name. Here is a sample returned value from the server:

    jQuery19008618064667098224_1372877021660 (
    {"key1":"value1","key2":"value2"})

    Note that it is a javascript code and function name if different for each call to avoid caching. In case you want to use cached response you can use lower lavel api jQuery.ajax() with cache=true.

    Jsonp – security

    Some comments on security:

    1. Here the server side code is written with the intention that it can be used with jsonp. So there is no security issue if server side code is written carefully.
    2. We should avoid serving any sensitive data like user personal data from server side jsonp code.
    3. We should also avoid any update logic in jsonp server side code.

    Few points to note

    1. An upcoming modern alternative to jsonp is to use Cross origin resource sharing. It is still not supported by all browsers though.
    2. Jsonp works only for get requests as it uses javascript <script> tag for the request.
    3. On a side note, flash uses cross domain policy files  to control cross domain requests access.

     

    Suggested posts:

    1. CSS – :nth child, :nth-last-child, :first-child, :last-child pseudo classes examples
    2. ReactJS – convert jsx to javascript using babel cli
    3. Handle XSS restriction using different domain for user entered javascript
    4. CSS – align text in center horizontally
    5. CSS attribute presence and value selectors
    6. React basic clock example using setInterval
    7. www vs non-www domain which is better for your site?
    8. How to switch back to old inline gmail compose window
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Javascript, jQuery, jQuery Ajax, Tutorials, Web Development
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress