我尝试使用 jQuery 1.9 发送 POST-JSON-Request。
我总是在控制台中收到以下错误。
类型错误:this.products 未定义。this.products.push(oneProduct);
这是我的代码
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" charset="utf-8" src="resources/js/model/Product.js"></script>
<script>
function getdetails(){
var p = new Product(15,11.5,"Pizza Test","P");
var z = new Product(68,1.5,"Zutate Test","Z");
p.addOneProduct(z);
var request = $.ajax({
type: "POST",
url: "yb_test_post.php",
dataType: "json",
data: p
});
request.done(function(msg) {
$("#msg").html( " Post parameter are: <br/>"+msg );
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
</script>
和 Product.js
function Product(id, price, name, type){
this.id = id;
this.price = +price;
this.totalPrice = +price;
this.name = name;
this.type = type;
this.products = [];
this.addOneProduct = function(oneProduct){
this.products.push(oneProduct);
this.totalPrice= this.totalPrice+oneProduct.price;
};
}
我究竟做错了什么?