Jquery $.Ajax() keep getting 401 response

痴心易碎 提交于 2020-01-07 08:22:32

问题


I'm trying to get JSON through an API but it keeps giving me

Response for preflight has invalid HTTP status code 401

This is my code

        $.ajax({
        beforeSend: function (request)
        {
            request.withCredentials = true;
            request.setRequestHeader("Authorization", "Basic" +  btoa("usernamehere:keyhere"));
        },
        crossDomain: true,
        contentType: 'application/json',
        url: url,

With the username and key different ofcourse. I've tried without btoa, i've tried using headers: and I've edited the CORS. But nothing works, it just keeps throwing the same error over and over eventhough I can login with the credentials if I go to the url.


回答1:


contentType: 'application/json'

Was not allowed, that fixed it.



来源:https://stackoverflow.com/questions/36764742/jquery-ajax-keep-getting-401-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!