【问题标题】:JQuery $.post() and curlJQuery $.post() 和 curl
【发布时间】:2016-04-29 22:17:51
【问题描述】:

这就是 GroupMe API (https://dev.groupme.com/docs/v3#messages_create) 的要求:

$ curl -X POST -H "Content-Type: application/json" -d '{"source_guid": "frgfre", "text":"alala"}' https://api.groupme.com/v3/groups/ID/messages?token=YOUR_ACCESS_TOKEN

请假设 ID 是有效的组 ID,并且令牌也有效且有效。我将如何将其转换为 $.post() 请求并从浏览器的控制台运行它?这是启用跨域并且它是受信任站点时在 IE 中不起作用的内容:

var t = {"source_guid": "frgfre", "text":"alala"};
$.post("https://api.groupme.com/v3/groups/ID/messages?token=YOUR_ACCESS_TOKEN", t);
//I have also tried t.toString() as well but it didn't work

如果无法转换(或者我现在拥有的是正确的),我将在哪里运行第一段代码?

【问题讨论】:

  • 以什么方式“不工作”?您是否收到跨域错误? GroupMe 是否支持 JSONP?
  • 我收到内部服务器错误。获取请求可以通过,不需要任何附件的帖子也是如此。

标签: javascript jquery curl


【解决方案1】:

$.postapplication/x-www-form-urlencoded 格式发布数据。如果 API 要求为 JSON,则需要使用 $.ajax 覆盖默认值。

$.ajax({
    url: "https://api.groupme.com/v3/groups/ID/messages?token=YOUR_ACCESS_TOKEN",
    data: JSON.stringify(t),
    contentType: 'application/json',
    processData: false
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-25
    • 2018-06-13
    • 2011-01-09
    • 2018-09-26
    • 1970-01-01
    相关资源
    最近更新 更多