显示“Uncaught SyntaxError:Unexpected token:”的AJAX Post Request调用

时间:2017-08-30 17:50:35

标签: jquery ajax

我正在尝试使用简写的AJAX Post Request 添加狗到我的API。然而,它并不喜欢我有一个":"在我的帖子请求中。

这是我的帖子请求功能:

    //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"
      })
    }

My Postman 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 个答案:

答案 0 :(得分: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/