weixin_33709609 2015-11-16 12:22 采纳率: 0%
浏览 91

与REST API的连接失败

have a problem when i am trying to connect and do a really sample GET request on a REST API.

Here is my code:

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

My problem is, this is returning a 401 error when i am trying to connect to the API on my JS but it's working when i am doing it directly on the website. So I think there is a bug with my data token line.

Any ideas why ?

Thanks a lot for taking time to answer me.

Rayden

  • 写回答

1条回答 默认 最新

  • weixin_33735676 2015-11-16 12:42
    关注

    I think there is problem with header Token modify it accordingly. I tried with curl by sending it in header. add this custom header

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

    This works for me.

    {
    "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);
    }
    

    });

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀