1

我试图从过去 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:&nbsp; <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: &nbsp; &nbsp; &nbsp; <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] “选项 /map/v1/users HTTP/1.1”204 0 546 546

我可以使用 Chrome 的 REST 控制台进行发布,但是在使用 jQuery 并提交表单时,服务器没有给出 201 响应。

4

0 回答 0