我试图在过去24小时内向我的客户端获取JSON有效负载但是徒劳无功。我的服务器的HTTP响应为204.我的代码:
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="user.css">
</head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script src="//code.jquery.com/jquery.js"></script>
<h1> User Sign-Up Form
</h1>
<div id="Registration-Page" class="User-form">
<form id="signupForm" action="" method="POST">
First name: <input type="text" name="firstName"><br>
Last name: <input type="text" name="lastName"><br>
SJSU ID: <input type="text"name="sjsuId"><br>
Password: <input type="password" name="password"><br>
Gender: <input type="radio" name="sex" value="male">Male
<input type="radio" name="sex" value="female">Female<br>
E-mail: <input type="email" name="email"><br>
Phone Number: <input type="text" name="phoneNumber"><br>
<input type="submit" name="submit" value="Submit" onClick="a()" />
</form>
<h2>JSON</h2>
</div>
<script type="text/javascript">
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push){
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$(function() {
$('#signupForm').Submit(function() {
$('#result').text(JSON.stringify($('signupForm').serializeObject()));
return false;
});
});
function a(){
var url = 'http://localhost:8080/map/v1/users';
var jsonData = JSON.stringify($('#signupForm').serializeObject()) //this in our case would be JSON text
var client = new XMLHttpRequest();
var parseJson= JSON.parse(jsonData);
client.open("POST", 'http://localhost:8080/map/v1/users',true);
client.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); //this will also change to JSON
alert(jsonData);
client.send(jsonData);
if (client.status == 201)
{
alert("The request succeeded!\n\nNew User Created");
window.location.href="userform.html"
}
else
alert("The request did not succeed!");
alert(jsonData);
alert(client.status);
alert(client.readyState);
};
</script>
</body>
我无法使用此方法将数据发布到我的服务器。我无法弄清楚为什么。在服务器端获得以下响应:
0:0:0:0:0:0:0:1 - - [12 / Nov / 2013:07:53:43 +0000]“OPTIONS / map / v1 / users HTTP / 1.1”204 0 546 546
我可以使用Chrome的REST控制台进行发布,但在使用jQuery并提交表单时,服务器没有给出201响应。