在jquery中发布请求

时间:2017-07-11 04:38:50

标签: jquery api

我如何在jquery中为此API执行post请求:

if($data3["Col"] < 2 ){

      $db->query("UPDATE answers SET Col = Col+1");

}elseif( $data3["Col"]= 2){// not $data3["Col"]= 2 but this $data3["Col"] == 2
      $db->query("UPDATE answers SET Col = Col+0");
  }

在我之前的代码中,我正在使用get请求它工作正常。所以现在我需要删除完整的get请求,我需要调用以下API的post请求。所以基本上我需要调用一个帖子请求,我需要将该数据下载到csv文件。下载到csv代码工作正常。请帮助我如何做以下API的发布请求。我的代码如下所示:

POST https://www.googleapis.com/doubleclickbidmanager/v1/lineitems/downloadlineitems?fields=lineItems&key={YOUR_API_KEY}

2 个答案:

答案 0 :(得分:0)

查看此链接https://api.jquery.com/jquery.post/以了解如何使用jquery POST

按要求编辑:发送请求数据,如下所示:

        var postData = { 
              fields: "lineItems",
              key: "{YOUR_API_KEY}"
        }
        $.post('https://www.googleapis.com/doubleclickbidmanager/v1/lineitems/downloadlineitems',postData, function(data) {
            console.log(data);
            for(var k = 0; k < data.items.length; k++) {
              youtubeData.push({
                "videoid": data.items[k].id,
                "Id": data.items[k].snippet.categoryId,
                "ChannelId": data.items[k].snippet.channelId,
                "Title": data.items[k].snippet.title,
                // "Description": data.items[i].snippet.description.replace("\n", ""),
                "Commentcount": data.items[k].statistics.commentCount,
                "ViewCount": data.items[k].statistics.viewCount,
                "LikeCount": data.items[k].statistics.likeCount,
                "DislikeCount": data.items[k].statistics.dislikeCount,
                "FavoriteCount": data.items[k].statistics.favoriteCount
              });
            }
            var csv = convertArrayOfObjectsToCSV({
                data: youtubeData
            });
            if (csv == null) return;

            filename = args.filename || 'export.csv';

            if (!csv.match(/^data:text\/csv/i)) {
                csv = 'data:text/csv;charset=utf-8,' + csv;
            }
            data = encodeURI(csv);

            link = document.createElement('a');
            link.setAttribute('href', data);
            link.setAttribute('download', filename);
            link.click();
        });

答案 1 :(得分:0)

尝试用此

替换$.getJSON
$.ajax({
    type : 'POST',
    url  : 'https://www.googleapis.com/youtube/v3/captions?part=snippet&videoId=PRU2ShMzQRg&key=""'
})
.done(function(data) {
    console.log(data);
    for(var k = 0; k < data.items.length; k++) {
        youtubeData.push({
            "videoid": data.items[k].id,
            "Id": data.items[k].snippet.categoryId,
            "ChannelId": data.items[k].snippet.channelId,
            "Title": data.items[k].snippet.title,
            // "Description": data.items[i].snippet.description.replace("\n", ""),
            "Commentcount": data.items[k].statistics.commentCount,
            "ViewCount": data.items[k].statistics.viewCount,
            "LikeCount": data.items[k].statistics.likeCount,
            "DislikeCount": data.items[k].statistics.dislikeCount,
            "FavoriteCount": data.items[k].statistics.favoriteCount
        });
    }
});