我们知道浏览器因为考虑到安全问题一般情况下是不支持跨域提交form表单了,但有时我们工作又需要了,下面就来看看一篇跨域提交form表单例子。
远程出于安全因素考虑,直接跨域访问是不允许的,下面介绍二种跨域的方法。
一,通过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"));
以前很多人用curl来抓,邮箱的通讯录,不过现在已经不可以了。哈哈。
二,利用jquery form,ajax提交
1,下载jquery.form.js
2,js代码
$('#testform').submit(function() {
$(this).ajaxSubmit({
type: 'post', // 提交方式 get/post
dataType:"json",//数据类型
url: 'your url', // 需要提交的 url
success: function(data) { // data 保存提交后返回的数据,一般为 json 数据
// 此处可对 data 作相关处理
alert('提交成功!');
}
$(this).resetForm(); // 提交后重置表单
});
return false; // 阻止表单自动提交事件
});
3,php代码
header("Access-Control-Allow-Origin:*"); //跨域权限设置,允许所有
header("Access-Control-Allow-Origin:http://www.111cn.net"); //只允许111cn.net跨域提交数据

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use