Home >Web Front-end >JS Tutorial >Analysis of ajax cross-domain method examples in jquery_jquery

Analysis of ajax cross-domain method examples in jquery_jquery

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 15:24:461169browse

This article analyzes ajax cross-domain in jquery through examples. Share it with everyone for your reference, the details are as follows:

JSONP is an unofficial protocol that allows integrating Script tags on the server side and returning them to the client, enabling cross-domain access in the form of javascript callback

Method 1: jsonp's getJSON

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

<&#63;php 
  $age=$_GET["age"];
  $name=$_GET["name"];
  $jsondata = "{age:$age, name:'$name'}";
  echo $_GET['callback'].'('.$jsondata.')';
&#63;>

Two jsonp of $.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

<&#63;php
  $age=$_GET["age"];
  $name=$_GET["name"];
  $ary=array("sd"=>"sdfg","aa"=>23);
   $jsondata=json_encode($ary);
  echo $_GET['callback5'].'('.$jsondata.')';
&#63;>

I hope this article will be helpful to everyone in jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn