This article mainly introduces the ajax cross-origin method in jquery, and analyzes the usage of getJSON and $ in jQuery with examples. ajax cross-origin implementation skills. if you need ajax cross-origin skills, refer to the examples in this article to analyze ajax cross-origin in jquery. We will share this with you for your reference. The details are as follows:
JSONP is an unofficial protocol that allows the server to integrate Script tags to return to the client and implement cross-origin access through javascript callback.
Method 1: getJSON of jsonp
Js
var url = "http://localhost/mytest/jsonp_php.php?callback=?";$.getJSON(url, { "age": 21, "name": "kitty"}, function (data) { alert("name:" + data.name + ", age:" + data.age);});
Php
<?php $age=$_GET["age"]; $name=$_GET["name"]; $jsondata = "{age:$age, name:'$name'}"; echo $_GET['callback'].'('.$jsondata.')';?>
Jsonp $. ajax
Js
$.ajax({ type: 'GET', url: 'http://localhost/mytest/jsonp_php.php', dataType: "jsonp", jsonp: "callback5", jsonpCallback:"flightHandler", data: { "age": 21, "name": "kitty" }, success: function (data) { alert("name:" + data.sd + ", age:" + data.aa) }})
Php
<?php $age=$_GET["age"]; $name=$_GET["name"]; $ary=array("sd"=>"sdfg","aa"=>23); $jsondata=json_encode($ary); echo $_GET['callback5'].'('.$jsondata.')';?>
I hope this article will help you with jQuery programming.