简体   繁体   English

使用jQuery.ajax请求和jsonp进行基本身份验证

[英]Basic Authentication with jQuery.ajax request and jsonp

I have some local html/js files with which I'd like to invoke some remote servers via https and eventually use Basic Authentication for the request. 我有一些本地html / js文件,我想通过https调用一些远程服务器,并最终使用基本身份验证的请求。

I am encountering two problems. 我遇到两个问题。 First is that if I don't specify 'jsonp' for the dataType, jQuery.ajax() request returns the error: 首先,如果我没有为dataType指定'jsonp',jQuery.ajax()请求将返回错误:

Access to restricted URI denied code: 1012 访问受限制的URI拒绝代码:1012

Are my requests considered cross-domain because my main work file is stored locally, but retrieving data from a server elsewhere? 我的请求是否被视为跨域,因为我的主要工作文件存储在本地,但是从其他地方的服务器检索数据?

So fine, I update the call so it now looks like: 很好,我更新了呼叫,现在它看起来像:

$.ajax({ 
     url: myServerUrl,
     type: "GET", 
     dataType: "jsonp", // considered a cross domain Ajax request if not specified
     username: myUsername,
     password: myPassword,

     success: function(result)
     {
        // success handling
     },
     error: function(req, status, errThrown){
         // error handling
     }
})

Because I need to use Basic Authentication, I'm passing in the username/password but if I monitor the request, I don't see it being set and additionally, the server sends an error response since it doesn't have the expected info. 因为我需要使用基本身份验证,我传递用户名/密码,但如果我监视请求,我看不到它被设置,此外,服务器发送错误响应,因为它没有预期的信息。

Additionally, because I have jsonp set, beforeSend won't get invoked. 另外,因为我有jsonp设置,所以不会调用beforeSend

How do I pass along the credentials using Basic Authentication for this request? 如何使用基本身份验证为此请求传递凭据?

The short version is you can't do this. 简短版本是你不能这样做的。 Your suspicions are correct, because you're local and these files are remote, you can't access them, you're being blocked by the same-origin policy . 您的怀疑是正确的,因为您是本地的,并且这些文件是远程的,您无法访问它们,您被同源策略阻止。 The work-around for that is JSONP , but that really doesn't seem to apply to your situation... 解决这个问题的方法是JSONP ,但这似乎并不适用于你的情况......

JSONP works differently, it's a GET request via a <script> tag include to get the file, so you're not sending special headers or anything. JSONP的工作方式不同,它是通过<script>标记包含的GET请求来获取文件,因此您不会发送特殊标题或任何内容。

You'll need to proxy the request through the server you're on (the domain of where this script is running) or another proxy option, but going from the client to another domain is blocked, mainly for security reasons. 您需要通过您所在的服务器(运行此脚本的域)或其他代理选项代理请求,但主要出于安全原因阻止从客户端到另一个域。

Try doing http://user:password@restservice . 尝试使用http:// user:password @ restservice This mimics a basic-auth request. 这模仿了基本身份验证请求。

I think you'll have to add a server proxy of some sort. 我想你必须添加某种服务器代理。 JSONP is just a particular way to use a script tag. JSONP只是使用脚本标记的一种特殊方式。 Thus, it doesn't allow setting arbitrary headers. 因此,它不允许设置任意标头。 And of course, you can't do a cross-origin XHR. 当然,你不能做一个跨性别的XHR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM