如果接口是php语言:
<?phpheader("Access-Control-Allow-Origin: *");
$name = isset($_POST['name'])? $_POST['name'] : '';
$gender = isset($_POST['gender'])? $_POST['gender'] : '';
$filename = time().substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'],'.'));
$response = array();
if(move_uploaded_file($_FILES['photo']['tmp_name'], $filename)){undefined
$response['isSuccess'] = true;
$response['name'] = $name;
$response['gender'] = $gender;
$response['photo'] = $filename;
}else{undefined
$response['isSuccess'] = false;
}
echo json_encode($response);
如果是框架比如thinkphp可以直接加在入口文件index.php
header("Access-Control-Allow-Origin: *");
如果接口不能修改,客户端也可以解决当然就是不是通过js提交给接口了,思路是先让js提交给自己的方法 ,然后通过自己的方法调取另外一个域名的接口(js请求会跨域,php程序请求不会跨域的,需要注意的是js跨域可以把信息提交给接口但是无法获取接口的返回值)