Ajax-Cross-Origin

Source: Internet
Author: User

I. jsonp

In fact, I thought that jsonp could use ajax to access other people's program code at will, but I found that it was not what I imagined, because jsonp had to change the server code. How can someone else's server code be changed? Unless others want to, You still cannot use ajax to obtain others' data.

There is a problem that AJAX directly requests common files with no cross-origin access permission. Render Manager determines that you are a static page, dynamic page, Web Service, and WCF. If you are a cross-origin request, no permission is allowed; in fact, the principle of jsonp is to remotely execute Js.

 
<SCRIPT type = "text/JavaScript" src = "remote JS file"> </SCRIPT>

Example-server code:

Namespace ajaxdomain. controllers {public class homecontroller: controller {public actionresult index () {return view ();} public actionresult getperson () {person P = new person (); p. id = 1; p. name = "Liu Bei"; p. age = 24; javascriptserializer JSS = new javascriptserializer (); string JSON = JSS. serialize (p); // serialized to JSON string callback = request ["Callback"]; // concatenate string call = callback + "(" + JSON + ")"; // callback concatenates brackets and the inherited JSON return content (CALL) ;}} public class person {public int ID {Get; Set ;}public string name {Get; set ;} public int age {Get; Set ;}}}

Modify the hosts table:

127.0.0.1 www.baidu.com

Front-end Page code:

<HTML> // Cross-origin Ajax (jsonp) $. ajax ({type: "Get", async: false, URL: "http://www.baidu.com: 8881/home/getperson", datatype: "jsonp", jsonp: "Callback ", // The parameter name passed to the request handler or page to obtain the jsonp callback function name (usually the default value is callback) // jsonpcallback :"? ", // Custom jsonp callback function name. The default value is the random function name automatically generated by jquery. You can also write it "? ", Jquery automatically processes the data for you. Success: function (p) {// var P = GetObject (JSON); // pay attention to the following, after jsonp is returned, jquery will be directly converted to a JS object, and no further conversion is required. $ ("# Div1 "). text ("name:" + P. name + "Age:" + P. age); alert (P. name) ;}, error: function () {alert ('cross-origin failed! ') ;}}) Function GetObject (STR) {return eval ("(" + STR + ")");} </SCRIPT>  

The page output results are as follows:

2. proxy access

Proxy access is actually to get data through the back-end Code such as. net, Java, PHP, and then return. There is nothing to say about this. You just need to download the page string from a WebClient and send it to the front-end.

Iii. IFRAME Method

This is suitable for mutual access between second-level domains of the same primary domain name and the primary domain name. For example, for Ajax interaction between www.a.com and blog.a.com, add document. Domain = "a.com" to the pages in both domains.

Second, the IFRAME cross-domain between different primary domain names:

Namespace MVC ___ learning test. controllers {public class homecontroller: controller {public actionresult index () {return view () ;}// simulate the cross-domain page. assume that this is the public actionresult getdata () page for different domain names () {return view ();} public actionresult getperson () {person P = new person (); p. id = 1; p. name = "Liu Bei"; p. age = 24; return JSON (p, jsonrequestbehavior. allowget) ;}} public class person {public int ID {Get; Set ;}public string name {Get; Set ;}public int age {Get; Set ;}}}

Getdata View:

<HTML>  

Index View:

<HTML>  

Open Path: http: // localhost: 5908/

The output is as follows:

Related Article

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.