PHP AJAX Jsonp Implementing cross-Domain Request usage instances

Source: Internet
Author: User
Tags php language

Before I wrote "PHP return JSON data Simple instance", "PHP returns JSON data in Chinese display problem" and "use JSON in PHP language and restore JSON to an array". Interested children's shoes can see

Today I am writing an instance of PHP AJAX Jsonp used. I don't know what JSONP is, please search by yourself

Example 1

Test.html

<!doctype html>

Ajax.js

$.ajax ({type: "post", url: "ajax.php", DataType: "Jsonp", Jsonp: "Callback",//passed to the request handler or page, to obtain the name of the JSONP callback function name (default: Callback) Jsonpcallback: "Success_jsonpcallback",//Custom JSONP callback function name, default to jquery automatically generated random function name Success:function (JSON) { Alert (' success ');},error:function () {alert (' fail ');}});

ajax.php

<?php $data = "..."; $callback = $_get[' callback '];echo $callback. ' ('. Json_encode ($data). ') '; Exit;? >

Jquery-1.5.2.min.js

Download your own Internet

When using JSONP, the function is called with JSONP form, such as "myurl?callback=?" JQuery is automatically replaced? is the correct function name to execute the callback function.

Example 2

Test.html

<!doctype html>

Ajax.js

$ (document). Ready (function () {    $ ("#btn"). Click (function (k) {        //...        var j = $ ("form"). Serializearray ();//serialization of Name/value        $.ajax ({type: ' GET ',  //here with Geturl: ' ajax.php ',            DataType: ' Jsonp ',  //Type DATA:J,JSONP: ' Callback ',//jsonp callback parameter, required            async:false,            success:function (Result) {//Return JSON Data                alert (result.message);//callback output                result = Result | | {};                if (result.msg== ' err ') {                    alert (result.info);                } else if (result.msg== "OK") {                    alert (' Commit succeeded ');                } else{                    alert (' Submission failed '),                }            },timeout:3000        })        //...    });

ajax.php

<?php$callback = Isset ($_get[' callback ')? Trim ($_get[' callback '): "; The JSONP callback parameter, required for the = Array ("Age" of =>$_get[' age '), "message" =>$_get[' age '), $date ["MSG"]= "err"; $date ["Info"]= "Due to character problem, send failed"; $tmp = Json_encode ($date); echo $callback JSON data. ' ('. $tmp. ') ';  Return format, required?>

Jquery-1.5.2.min.js

Download yourself from the Internet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.