一、js:
- 要點:
- 完整程式碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="jquery.min.js"></script>
</head>
<body>
<script>
$.ajax({
`url`:`http://localhost/index.php`,
`data`:{`sex`:`male`},
`beforeSend`:function(xhr){
xhr.setRequestHeader(`name`,`lee`);
xhr.setRequestHeader(`sex`,`male`);
xhr.setRequestHeader(`height`,`168`);
},
`success`:function(data){
console.log(data);
}
});
</script>
</body>
</html>
二、php:
- 要點:
- 完整程式碼:
<?php
header(`Access-Control-Allow-Origin:*`);
header(`Access-Control-Allow-Credentials: true`);
header(`Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS`);
header(`Access-Control-Allow-Headers:WWW-Authenticate,Authorization,Set-Cookie,X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version,name,sex,height`);
$header = apache_request_headers();
var_Dump($header);exit;
本文轉自 Lee_吉 51CTO部落格,原文連結:http://blog.51cto.com/12173069/2066641