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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记