Examples of ajax cross-origin method in jquery _ jquery

Source: Internet
Author: User
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.

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.