一切似乎都很好。我也检查了JSON验证器 我们传递的数据类型是有效的。但是我收到以下错误
POST http://xx.xxx.xxx.xxx:3001/createData 500(内部服务器错误)jquery.min.js:2
<html>
<head>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<h2>HTML Forms</h2>
</head>
<body>
<form name = "test_form" method="post">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="button" onclick="goodCall()" value="Submit" >
</form>
</body>
<p>If you click the "Submit" button, the form-data will be sent to a page called "/action_page.php".</p>
<script>
function goodCall() {
//var url = http://34.201.147.118:3001/createData
var data = {
"productID": document.test_form.firstname.value,
"title": document.test_form.lastname.value,
}
dataset = JSON.stringify(data)
console.log(typeof dataset);
console.log(dataset);
$.ajax({
type: "POST",
url: "http://34.201.147.118:3001/createData",
data: dataset,
success: alert("your data has been sucefull posted"),
dataType: 'application/json',
});
};
</script>
</html>
我的输出
的字符串
stackoverflow.html:29
{&#34;&的productID#34;:&#34;米奇&#34;&#34;标题&#34;:&#34;小鼠&#34;}
接下来我收到了错误
jquery-3.3.1.min.js:2 POST http://34.201.147.118:3001/createData 500 (Internal Server Error)
send @ jquery-3.3.1.min.js:2
ajax @ jquery-3.3.1.min.js:2
goodCall @ stackoverflow.html:30
onclick @ stackoverflow.html:15
答案 0 :(得分:1)
更像这样:
function goodCall() {
var url = "http://xx.xxx.xxx.xxx:3001/createData"
var data = {
"productID": document.test_form.firstname.value,
"title": document.test_form.lastname.value,
}
dataset = JSON.stringify(data)
console.log(typeof dataset);
console.log(dataset);
$.post(
url: url,
data: data,
success: sucess_func()
);
};
有关参数的更多信息: https://api.jquery.com/jquery.post/
...或使用.ajax方法:
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
答案 1 :(得分:1)
尝试这种方式:
function goodCall() {
var data = {
"productID": document.test_form.firstname.value,
"title": document.test_form.lastname.value,
}
$.ajax({
url: "http://34.201.147.118:3001/createData",
type: "POST",
data: {data:data},
dataType: 'json',
success: function(response){
alert("your data has been sucefull posted");
},
error:function(err){
alert("ERROR");
}
});
};