Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 与REST API的连接失败_Json_Ajax_Api_Rest - Fatal编程技术网

Json 与REST API的连接失败

Json 与REST API的连接失败,json,ajax,api,rest,Json,Ajax,Api,Rest,当我尝试连接并在RESTAPI上执行一个真正的示例GET请求时出现问题 这是我的密码: $.ajax({ url : 'https://recruitment.elements.nl:8080/v1/carousel/', type : "GET", dataType: "json", data : { token : '733129ed5566ed9b030872c6e003155a82da3c55' }, success : function(data) { alert

当我尝试连接并在RESTAPI上执行一个真正的示例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错误,但当我直接在网站上连接API时,它工作正常。 所以我认为我的数据令牌行有一个bug

你知道为什么吗

非常感谢您抽出时间回答我的问题


Rayden

我认为标头标记有问题 相应地修改它。我试着用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/"
    }
]
}


})

我认为您应该将令牌作为头发送,而不是作为dataHi@AvsenevSlava发送,我看到您最近做了很多编辑,将内容放入代码标记中。这些东西中有很多实际上并不属于代码标签——GUI或属性等术语可以独立存在。请谨慎行事,如有必要,请查看。谢谢
$.ajax({
url: 'YourRestEndPoint',
headers: {
    'Authorization':'Token xxxxxxxxxxxxx'
},
method: 'GET',
dataType: 'json',
data: YourData,
success: function(data){
  console.log('succes: '+data);
}