【问题标题】:Connection to REST API failed连接到 REST API 失败
【发布时间】:2015-11-16 12:22:55
【问题描述】:

当我尝试连接并在 REST API 上执行真正的示例 GET 请求时遇到问题。

这是我的代码:

 $.ajax({
 url : 'https://recruitment.elements.nl:8080/v1/carousel/',
 type : "GET",
 dataType: "json",
 data : { token : '733129ed5566ed9b030872c6e003155a82da3c55' },
 success : function(data) { 
    alert('data');
 },
 error : function(xhr) { }
});

我的问题是,当我尝试连接到我的 JS 上的 API 时,这会返回 401 错误,但是当我直接在网站上执行此操作时它正在工作。 所以我认为我的数据令牌行存在错误。

任何想法为什么?

非常感谢您花时间回答我。

雷登

【问题讨论】:

  • 我认为您应该将令牌作为标头发送,而不是作为数据发送
  • 嗨@AvsenevSlava,我看到你最近做了很多编辑,把东西放到code 标签中。很多这些东西并不真正属于代码标签——像“GUI”或“属性”这样的术语可以独立存在。请谨慎行事,如有必要,请查看editing guidelines。谢谢!

标签: json ajax api rest


【解决方案1】:

我认为header Token有问题 相应地修改它。我通过在标题中发送它来尝试使用 curl。添加此自定义标题

curl -X GET https://recruitment.elements.nl:8080/v1/carousel/ -H 'Authorization: Token 733129ed5566ed9b030872c6e003155a82da3c55'

这对我有用。

{
"count": 3,
"next": null,
"previous": null,
"results": [
    {
        "id": 1,
        "image": "https://recruitment.elements.nl:8080/media/carousel/caroussel_1.png",
        "image_2x": "https://recruitment.elements.nl:8080/media/carousel/caroussel_12x.png",
        "title": "Little tittle",
        "subtitle": "Big Title",
        "action": "Button Action",
        "action_url": "https://www.elements.nl/portfolio/mcdonalds-picto-campaign/"
    },
    {
        "id": 2,
        "image": "https://recruitment.elements.nl:8080/media/carousel/caroussel_2.png",
        "image_2x": "https://recruitment.elements.nl:8080/media/carousel/caroussel_22x.png",
        "title": "NU.nl responsive website",
        "subtitle": "NEW WEBSITE",
        "action": "GO!",
        "action_url": "https://www.elements.nl/portfolio/nu-nl-responsive-website/"
    },
    {
        "id": 3,
        "image": "https://recruitment.elements.nl:8080/media/carousel/caroussel_3.png",
        "image_2x": "https://recruitment.elements.nl:8080/media/carousel/caroussel_32x.png",
        "title": "don't text and cycle",
        "subtitle": "Fietsmodus app",
        "action": "go to project",
        "action_url": "https://www.elements.nl/portfolio/fietsmodus/"
    }
]

}

$.ajax({
url: 'YourRestEndPoint',
headers: {
    'Authorization':'Token xxxxxxxxxxxxx'
},
method: 'GET',
dataType: 'json',
data: YourData,
success: function(data){
  console.log('succes: '+data);
}

});

【讨论】:

    猜你喜欢
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 2015-01-18
    相关资源
    最近更新 更多