0
我編寫了一個基於POST請求返回數據的API。Javascript/jQuery發佈問題
當我使用HTML表單時,它可以工作。當我使用wget和post-data時,它可以工作,但不管我使用什麼javascript代碼,我都無法使它工作。
因此,我使用了Chrome開發板,並查看網絡選項卡,它顯示py文件響應,並顯示正確的響應,它只是不顯示在屏幕上。
我以爲是頭部,所以我的Python(API)有此作爲標題:
print "Content-Type: text/json"
print "Access-Control-Allow-Origin: *"
print "Access-Control-Allow-Methods: GET, POST, PUT, DELETE"
print "Access-Control-Expose-Headers: Access-Control-Allow-Origin"
print "Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"
的Javascript:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("http://www.example.com/cgi-bin/main.py",
{
uname:"username",
pword:"password"
},
function(data,status){
$(".data").html(data);
$(".status").html(status);
});
});
});
</script>
</head>
<body>
<button>Send</button>
<p>Data: <span class="data"></span></p>
<p>Status: <span class="status"></span></p>
</body>
</html>
檢查您的瀏覽器控制檯是沒有任何錯誤 –
「event.returnValue已被棄用,請使用標準event.preventDefault()代替。」 但是,這不應該影響它 – triunenature
點擊處理程序是否被調用?點擊按鈕後,還有任何額外的日誌記錄 –