我寫了一個簡單的代碼: 登錄頁面,我試圖發送用戶名到php頁面。不能POST,405方法不允許
當我點擊提交登錄頁面,我收到405方法不允許。
如果我使用get方法 - 它的工作,但我想發佈,因爲我想發送用戶和傳遞到服務器不在地址。
HTML文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<script src="mainjs.js"></script>
</head>
<body>
<form>
First name:<br>
<input id="fname" type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input id="lname" type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit" onclick="testfunc();">
</form>
<div id='result'></div>
<p>If you click the "Submit" button, the form-data will be sent to a page called "action_page.php".</p>
</body>
JavaScript:
function testfunc(){
alert("MainJS");
var fname = $('#fname').val();
var lname = $('#lname').val();
$.post('getdata.php', {fname:fname, lname:lname}, function(data){
alert(data);
$('#result').html(data);
});
}
PHP文件:
<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With,Content-Type, Accept");
header('Access-Control-Allow-Methods: GET, POST, PUT');
echo "Success";
?>
謝謝!