We know that the browser generally does not support cross-origin form submission because of security issues, but sometimes we need it again. Next let's take a look at an example of cross-origin form submission. We know that the browser generally does not support cross-origin form submission because of security issues, but sometimes we need it again. Next let's take a look at an example of cross-origin form submission.
Script ec (2); script
For security reasons, direct cross-origin access is not allowed. The following describes two cross-origin methods.
1. Use php curl
Function curlPost ($ url, $ params)
{
$ PostData = '';
Foreach ($ params as $ k => $ v)
{
$ PostData. = $ k. '='. $ v .'&';
}
Rtrim ($ postData ,'&');
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_HEADER, false );
Curl_setopt ($ ch, CURLOPT_POST, count ($ postData ));
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ postData );
$ Output = curl_exec ($ ch );
Curl_close ($ ch );
Return $ output;
}
Echo curlPost ("http://111cn.net", array ('name' => "tank "));
In the past, many people used curl to capture the email address book, but it is no longer possible. Haha.
Ii. Use jquery form and ajax to submit
1. Download jquery. form. js
2. js Code
$ ('# Testform'). submit (function (){
$ (This). ajaxSubmit ({
Type: 'post', // submit method get/post
DataType: "json", // data type
Url: 'Your url', // url to be submitted
Success: function (data) {// data stores the data returned after submission, which is generally json data
// Data can be processed here
Alert ('submission successful! ');
}
$ (This). resetForm (); // reset the form after submission
});
Return false; // prevents automatic form submission events
});
3. php code
Header ("Access-Control-Allow-Origin: *"); // cross-Origin permission settings, Allow all
Header ("Access-Control-Allow-Origin: http://www.111cn.net"); // only Allow 111cn.net cross-Origin data submission