0
我有這個代碼做http post,這個職位正在工作,但服務器端期望數據在json格式。它顯示我的帖子中的數據不是以json格式。如何更改我的代碼以json格式發佈數據?jquery json http post - 如何將發佈數據轉換爲json格式?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("http://www.example.com/post",
dataType: 'json',
{
"userID" :"JohnDoe",
"timeStamp" :""
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
for (var key in data) {
if (data.hasOwnProperty(key)) {
var val = data[key];
console.log(val);
}
}
});
});
});
</script>
</head>
<body>
<button>Send an HTTP POST request to a page and get the result back</button>
</body>
</html>
我試試你的代碼,這是行不通的。 – CK8
@ CK8有什麼問題呢? –
@ CK8我用一個工作端點更新了代碼 –