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

WBOY
Release: 2016-05-16 15:24:46
Original
1086 people have browsed it

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);
});

Copy after login

php

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

Copy after login

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)
  }
})

Copy after login

php

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

Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template