Jquery Post get cross-origin commit data

Source: Internet
Author: User
[Reprint jquery post cross-origin submission data

N cross-origin formats:

1. directly use $. getjson in jquery for cross-origin submission

Advantage: The returned value can be directly cross-origin;

Disadvantage: small data size;

Submission method: only get (no $. postjson)

2. embed an IFRAME in the page and set the width and height of the IFRAME to 0 for cross-origin submission.

Advantage: direct cross-origin is supported;

Disadvantage: no return value (out of the essence of Ajax );

Submission method: Get/post

3. use $. post cross-origin: first submit to the local domain name next agent, submit the post or GET request to the destination domain name through the proxy, and return the result to the local page through the proxy according to the returned value of the destination Domain Name

Advantage: The returned value can be used for cross-origin. You can use a proxy program to count user information such as IP addresses to increase security;

Submission method: Get/post

Complexity: frontend engineers and backend engineers are required to work together (PHP/Java ../engineers)

Disadvantage: local server resources need to be consumed to increase Ajax wait time (negligible)

4. Learn from Baidu: as calling any JS file does not involve cross-origin issues, JS scripts can write and call JS files on remote servers to implement the services you need.

That is, A. js dynamically calls www.baidu.com/ B .js, where B. js implements the business

5. To be studied ......

 

 

1. Use the post submission method

2. Construct the number format of the form

3. Call the Ajax callback function with the Form submit.

Code:

Submit a form asynchronously using jquery

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> No title page </title>
</Head>
<SCRIPT src = "JS/jquery-1.4.2.js"> </SCRIPT>
<SCRIPT>
Jquery (function ($ ){
// Use jquery to submit forms Asynchronously
$ ('# F1'). Submit (function (){
$. Ajax ({
URL: 'ta. aspx ',
Data: $ ('# F1'). serialize (),
Type: "Post ",
Cache: false,
Success: function (data)
{Alert (data );}
});
Return false;
});
});

</SCRIPT>
<Body>
<Form ID = "f1" name = "f1">
<Input name = "A1"/>
<Input name = "A2"/>
<Input id = "file1" type = "file" name = "file1"/>
<Input id = "submit1" type = "Submit" value = "Submit"/>
</Form>
</Body>
</Html>

How can I submit forms asynchronously?

1. Use the cross-origin access feature of the script and format the data in the Form. Therefore, only the get method can be used for submission. For security reasons, the browser does not support post cross-origin submission.

2. Using jsonp to submit forms across domains is a good solution.

3. Dynamic programs can also be used as a proxy. Use a proxy to transfer cross-origin requests.

Code:

Use jquery asynchronous cross-origin submission form

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> No title page </title>
</Head>
<SCRIPT src = "JS/jquery-1.4.2.js"> </SCRIPT>
<SCRIPT>
Jquery (function ($)
{
// Use jquery asynchronous cross-origin submission form
$ ('# F1'). Submit (function ()
{
$. Getjson ("ta. aspx? "+ $ ('# F1'). serialize () +" & jsoncallback =? ",
Function (data)
{
Alert (data );
});
Return false;
});
});

</SCRIPT>
<Body>
<Form ID = "f1" name = "f1">
<Input name = "A1"/>
<Input name = "A2"/>
<Input id = "file1" type = "file" name = "file1"/>
<Input id = "submit1" type = "Submit" value = "Submit"/>
</Form>
</Body>
</Html>

Supplement: method 1 cannot implement cross-commit.

Note: The output JSON format {'a1', 'a1value', 'a2 ': 'a2value '}

Characters must be enclosed in quotation marks, and numbers can be enclosed without quotation marks. For example: {'a1', 10, 'a2 ': 20}

 

 

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.