Cross-origin submission using jquery

Source: Internet
Author: User

Introduction: This is a detailed page for jquery to implement cross-origin submission. It introduces the knowledge, skills, and experience related to PhP, JSON, jsonp, jquery, JavaScript, and PHP jquery to implement cross-origin submission, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 344674 'rolling = 'no'>
Address: http://www.luocheng.cn/article-view-246.html

When operating a product, we often encounter such forms, such as customer data collection, application for joining, and feedback. Therefore, we developed such a product, it can be understood as the hichina data collection platform. It is not difficult to think that we will encounter cross-origin problems at the front end. For example, www.a.com needs to submit data to www.app.com. In the past, we used a server proxy to solve cross-origin problems, the last time I encountered a Windows server, it was quite troublesome to implement proxy, so I thought of jquery implementation. There are two methods: getjson and getscript.

Getscript is often used to obtain server data. getjson can be used to submit information. In fact, both of them implement cross-origin submission.

In jquery1.3.2, you can use a callback function in the form of jsonp to load JSON data of other domains, such as "myurl? Callback =? ". Will jquery be replaced automatically? For the correct function name to execute the callback function.

Jsonp principle:

Register a callback on the client, and then pass the callback name to the server.

In this case, the server is converted into JSON data.

Then, a function is generated in Javascript syntax. The function name is the passed parameter jsonp.

Finally, place the JSON data directly in the function as an input parameter. In this way, a JS syntax document is generated and returned to the client.

The client browser parses the script tag and executes the returned JavaScript document. As a parameter, the data is passed in to the client-defined callback function to dynamically execute the callback function.

Instance:

JqueryCode:

$. Getjson ("http://www.app.com/index.php? C = add & callback =? ", {ID: 10, name:" test "}, function (data ){

Alert (data. msg );

});

Server return:

Jsonp1321328945031 ({"Rs": True, "MSG": "u60a8u7684u4fe1u606fu63d0u4ea4u6210u529fuff01 "})

PHP code:

$ Result ['rs '] = false;

$ Result ['msg '] = 'your information has been submitted successfully! ';

$ JSON = new services_json ();

Header ('content-type: Application/json ');

Echo $ _ Get ['callback']. '('. $ JSON-> encode ($ result ).')';

Love J2EE follow Java Michael Jackson video station JSON online tools

http://biancheng.dnbcw.info/php/344674.html pageno: 4

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.