无法在我的JSON文件中发布数据

时间:2016-05-10 07:36:36

标签: javascript json post

无法在我的JSON文件上执行Post方法     我的JSON文件是     [{     “名字”:“帮助”,     “description”:“不允许删除产品!?”,     “价格”:100000.0}]

<html>
<head>
<title>Product Management</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/default.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"</script>
<script src="js/bootstrap.min.js"></script>

获取值的功能

<script>              
$(document).ready(function() {
    $(window).load(function() {
        $.ajax({
            type: 'GET',
            url: 'test.json',
            dataType: 'json',
            success: function(data) {
                $.each(data, function(index, element) {
                    $("#abc").append($('<li>', {
                        text: element.name
                    }))
                });
            }
        });
    });
});
</script>  

在JSON文件中发布值的功能

<script>   
function sumbitForm(){
var x=document.getElementById("#name");
var y=document.getElementById("#description");
var z=document.getElementById("#price");    
var Product={"name":x, "description":y, "price":z };
$.ajax({
url: "test.json",
type: "POST",
contentType: "application/json",
data: JSON.stringify(Product)
});
}
</script>   
</head>
<body>
<div>
<ol id="abc">
</ol>
</div>
<div class="container">             

发布数据的输入表单

<form  name="PForm" action="" method="POST">
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" id="name" placeholder="Product Name">
</div>
<div class="form-group">
<label>Description:</label>
<textarea class="form-control" id="description" placeholder="Descrpition" rows="8"></textarea>
</div>
<div class="form-group">
<label>Price:</label>
<input type="text" class="form-control" id="price" placeholder="Price">
</div>
<button type="submit" class="btn btn-primary Right" onClick="submitForm()">Submit</button>
</form>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您需要获取元素的,例如

var x=document.getElementById("#name").value;

否则你会发送DOM元素(转换为字符串,只会是&#39; object&#39;)