欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery中ajax跨域方法實(shí)例分析

 更新時(shí)間:2015年12月18日 11:30:54   作者:leejersey  
這篇文章主要介紹了jquery中ajax跨域方法,結(jié)合實(shí)例分析了jQuery中使用getJSON及$.ajax實(shí)現(xiàn)ajax跨域的技巧,需要的朋友可以參考下

本文實(shí)例分析了jquery中ajax跨域。分享給大家供大家參考,具體如下:

JSONP是一個(gè)非官方的協(xié)議,它允許在服務(wù)器端集成Script tags返回至客戶端,通過javascript callback的形式實(shí)現(xiàn)跨域訪問

方法一: jsonp之 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

<?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.')';
?>

希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論