weixin_33734785 2017-05-05 09:07 采纳率: 0%
浏览 16

$ .ajax帖子返回401 [重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/21783079/ajax-in-chrome-sending-options-instead-of-get-post-put-delete" dir="ltr">AJAX in Chrome sending OPTIONS instead of GET/POST/PUT/DELETE?</a>
                            <span class="question-originals-answer-count">
                                (11 answers)
                            </span>
                    </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/35553500/xmlhttprequest-cannot-load-xxx-no-access-control-allow-origin-header" dir="ltr">XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header</a>
                            <span class="question-originals-answer-count">
                                (8 answers)
                            </span>
                    </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it" dir="ltr">Why is an OPTIONS request sent and can I disable it?</a>
                            <span class="question-originals-answer-count">
                                (14 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2017-05-05 09:10:11Z" class="relativetime">2 years ago</span>.</div>
        </div>
    </aside>

Continue to receive this as a return for a $.ajax post.

"401 (API requests require HTTP Basic Auth or OAuth.) Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource."

Returns 200 OK with postman or hurl.it.

I have written the following below continue to see the above error. Should I be adding headers under url instead of in the beforeSend, or have I omitted an additional unknown parameter?

This is an ajax POST for http://api.myemma.com/ and the credentials and account ID being used are valid.

 var data = {
    "fields": {
    "first_name": "fname",
    "last_name": "lname",
    "postal_code": "00000"
    },
    "email": "test@testing.com",
},
username = "username",
password = "password",
accountid = "0000000";
$.ajax({
  type: "POST",
  url: "https://api.e2ma.net/"+accountid+"/members/add",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  data: JSON.stringify(data),
  xhrFields: {
    withCredentials: true
  },
  beforeSend:function(xhr){
    console.log("beforesend");
    xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
  },
  success:function(data){
    alert("success");
  },
  error:function(error){
    console.log(error);
  }
});
</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?