2017-08-30 96 views
-1

我试图使用速记AJAX Post请求添加一条狗到我的API。但是,我不希望我的帖子请求中有“:”。AJAX Post请求调用显示“未捕获的SyntaxError:意外的令牌:”

这是我的POST请求的功能:

//add(POST AJAX REQUEST) one dog to the api 
    function addOneDog (myUrl) { 
     $.post(myUrl, function(data) { 
     dogBreed: "Hound Dog", 
     dogName: "Freddy", 
     dogAge: 5, 
     dogColor: "White", 
     dogPersonality: "Angry" 
     }) 
    } 

我的邮差GET请求我的API:

[ 
    { 
     "dogBreed": "Border Collie", 
     "dogName": "Bob", 
     "dogAge": 2, 
     "dogColor": "Brown", 
     "dogPersonality": "Loyal", 
     "id": 3, 
     "createdAt": "2017-08-29T22:52:46.832Z", 
     "updatedAt": "2017-08-29T22:52:46.832Z" 
    } 
    ] 

我下面的jQuery的文档上显示的例子:

$.post("test.php", { name: "John", time: "2pm" }); 

有什么建议吗?我想我忘记了一个语法。

回答

1

只是一些语法错误,你把你的POST数据在错误的地方 - 你现在有它的回调函数(生成语法错误):

$.post(myUrl, { 
    dogBreed: "Hound Dog", 
    dogName: "Freddy", 
    dogAge: 5, 
    dogColor: "White", 
    dogPersonality: "Angry" 
}, function(data) { 
    //response is what data is 
}) 

https://api.jquery.com/jquery.post/

相关问题