Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JSONP Tutorial

JSONP Tutorial

In this chapter, we will introduce the knowledge of JSONP.

JSONP (JSON with Padding) is a "usage pattern" of JSON that allows a web page to obtain data from another domain (website), i.e., to read data across domains.

Why do we need a special technique (JSONP) to access data from different domains (websites)? It is because of the same-origin policy.

The same-origin policy is a famous security policy proposed by Netscape; now all browsers that support JavaScript use this policy.


JSONP applications

1. Server-side JSONP format data

For example, if a client wants to access: https://www.runoob.com/try/ajax/jsonp.php?jsoncallback=callbackFunction.

Suppose the client expects the returned data: ["customername1","customername2"].

The data actually returned to the client is displayed as: callbackFunction(["customername1","customername2"]).

The server-side file jsonp.php code is:

jsonp.php file code

2. Client-side implementation of the callbackFunction function

<script type="text/javascript" function callbackFunction(result,methodName){var html=''>

;

for

(

var

i

=

0

;

i

<

result

.

length

;

i

++

)

{

html

+=

'

  • '

    result

    [

    i

    ]

    '

  • '

    ;

    }

    html

    +=

    '

    '

    ;

    document

    .

    getElementById

    (

    '

    divCustomers

    '

    )

    .

    innerHTML

    =

    html

    ;

    }

    </
    
    script
    
    <
    
    div
    
    id
    
    =
    
    "
    
    divCustomers
    
    "
    
    </
    
    div
    
    <

    !

    DOCTYPE

    html

    <html>
    <head>
    <meta charset="utf-8">
    <title>

    JSONP example

    </
    
    title
    
    </
    
    head
    
    <
    
    body
    
    <
    
    div
    
    id
    
    =
    
    "
    
    divCustomers
    
    "
    
    </
    
    div
    
    <
    
    script
    
    type
    
    =
    
    "
    
    text/javascript
    
    "
    
    function
    
    callbackFunction
    
    (
    
    result
    
    ,
    
    methodName
    
    )
    
    {
    
    var
    
    html
    
    =
    
    '
    
    '

    ;

    for

    (

    var

    i

    =

    0

    ;

    i

    <

    result

    .

    length

    ;

    i

    ++

    )

    {

    html

    +=

    '

  • '

    result

    [

    i

    ]

    '

  • '

    ;

    }

    html

    +=

    '

    '

    ;

    document

    .

    getElementById

    (

    '

    divCustomers

    '

    )

    .

    innerHTML

    =

    html

    ;

    }

    </
    
    script
    
    <
    
    script
    
    type
    
    =
    
    "
    
    text/javascript
    
    "
    
    src
    
    =
    
    "
    
    https://www.runoob.com/try/ajax/jsonp.php?jsoncallback=callbackFunction
    
    "
    
    </
    
    script
    
    </
    
    body
    
    </
    
    html

    The above code can use a jQuery code example:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>

    JSONP example

    </
    
    title
    
    <
    
    script
    
    src
    
    =
    
    "
    
    https://cdn.static.runoob.com/libs/jquery/1.8.3/jquery.js
    
    "
    
    </
    
    script
    
    </
    
    head
    
    <
    
    body
    
    <
    
    div
    
    id
    
    =
    
    "
    
    divCustomers
    
    "
    
    </
    
    div
    
    <
    
    script

    $.

    getJSON

    (

    "

    https://www.runoob.com/try/ajax/jsonp.php?jsoncallback=?

    "

    ,

    function

    (

    data

    )

    {

    var

    html

    =

    '

    '

    ; $

    (

    '

    #divCustomers

    '

    )

    .

    html

    (

    html

    )

    ;

    }

    )

    ;

    </
    
    script
    
    </
    
    body
    
    </
    
    html