我在安全站点上有RESTful服务.我可以在Firefox中使用REST客户端建立一个请求,但是当我尝试在代码中构建它时,我得到一个401.这是我的代码
(function() {
$.ajax({
url : "https://myrestsite",
type : "GET",
//beforeSend: function (req){
// req.setRequestHeader("Authorization","myauthstring");
// req.setRequestHeader("Content-Type", "application/json");
//},
headers : {
"Authorization" : "myauthstring",
"Content-Type" : "application/json",
},
success : function (){alert('we got here')}
});
})();
Run Code Online (Sandbox Code Playgroud)
这显示在FF中的Web开发人员工具中:
Request-Headers:authorization,content-type
Run Code Online (Sandbox Code Playgroud)
当我需要(从其他客户端是FF)是:
Authorization:myAuthstring
Content-Type:application/json
Run Code Online (Sandbox Code Playgroud)
关于我做错了什么的线索?
**编辑 - 事实证明我正在达到跨域限制.其他客户如何解决这个问题?
我可以访问一个API,它是一个JSP文件并且是JSON格式的.我试图从JSP页面获取这些数据到PHP脚本并处理它们然后存储在我的MySQL服务器中.
JSON字符串在我在少量JSON Formatter和验证器在线检查的JSP页面中有效.
这是我用来从页面获取JSON数据的代码,但每次我的ajax调用失败时.
$('#button').click(function(e){
var url = 'http://xxxxx:8080/StudentAPI/index.jsp';
$.ajax({
url : url,
dataType : 'json',
success : function(response) {
alert('Success');
},
error : function(request, textStatus, errorThrown) {
alert(request+textStatus+errorThrown);
}
});
e.preventDefault();
})
Run Code Online (Sandbox Code Playgroud)
请帮助我,并建议以更好的方式做到这一点,我们总是欢迎.
如何在我的java脚本中加载一些网站以便我可以解析它?我想获得例如www.google.com的Html,我想使用jquery选择其中的所有标签.
我使用Yahoo Social API了Contacts通过JavaScript中使用OAuth2(这里给出https://developer.yahoo.com/oauth2/guide/#implicit-grant-flow-for-client-side-apps)
但是,经过successful authentication并纠正后Access Token,我无法通过JS完成调用.浏览器说:
XMLHttpRequest cannot load https://social.yahooapis.com/v1/user/me/contacts
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://..' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用相同的调用PHP的Curl时,它可以工作(因此证明它不是令牌的问题).
谁知道如何解决这个问题?谢谢.
我正在尝试访问Spotify API令牌,如下所示:
$.ajax({
url: "https://accounts.spotify.com/api/token",
type: 'POST',
contentType: "application/json; charset=\"utf-8\"",
crossDomain: true,
data: {
grant_type: "authorization_code",
code: code,
redirect_uri: "http://www.bancadigital.com.br/spotifyteste/callback.html"
},
processData: false,
dataType: "json",
headers: {
Authorization: "Basic " + utf8_to_b64(key)
},
success: function( response ) {
alert(response.access_token);
},
});Run Code Online (Sandbox Code Playgroud)
但该服务返回以下错误:
XMLHttpRequest无法加载https://accounts.spotify.com/api/token.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许原点' http://www.bancadigital.com.br '访问.
有谁知道我如何访问该服务?
我成功地将文件从 发送localhost:8888到localhost:8080(生产中的不同域),但在传输完成后我无法读取 HTTP 响应。
未捕获的安全错误:无法从“HTMLIFrameElement”读取“contentDocument”属性:阻止来源为“ http://localhost:8888 ”的框架访问来源为“ http://localhost:8080 ”的框架。请求访问的框架将“document.domain”设置为“localhost”,但正在访问的框架没有设置。两者都必须将“document.domain”设置为相同的值才能允许访问。
为了发送文件,为了兼容性支持,我试图让它适用于<form>基于文件上传;没有XHR根据。这是基本的 HTML 结构:
<form target="file-iframe" enctype="multipart/form-data" method="POST" action="invalid">
<input type="file" id="file-input" class="file-input" title="select files">
</form>
<iframe src="javascript:false;" id="file-iframe" name="file-iframe"></iframe>
Run Code Online (Sandbox Code Playgroud)
要将<iframe>元素插入到 DOM 中,我执行以下操作:
document.domain = document.domain;
var domainHack = 'javascript:document.write("<script type=text/javascript>document.domain=document.domain;</script>")';
var html = '<iframe id="file-iframe" name="file-iframe"></iframe>';
var parent = document.getElementById('wrapper');
var iframe = UTILS.createDomElement(html, parent);
iframe.src = domainHack;
UTILS.attachEvent(iframe, 'load', function(e) {
// this throws the above SecurityError
var doc = …Run Code Online (Sandbox Code Playgroud) 我正在使用HTML和JavaScript 构建小型离线 Web应用程序.CSV文件用于存储数据.
要读取CSV(使用jquery-csv库)文件,我使用以下代码.
<script>
$(document).ready(function(){
var filepath = 'data.csv';
var data_string = $.get(filepath);
});
</script>
Run Code Online (Sandbox Code Playgroud)
但由于跟随错误,我无法阅读它.
XMLHttpRequest无法加载file:/// C:/Users/Nimal/Desktop/javascript-csv/data.csv.交叉源请求仅支持协议方案:http,数据,chrome,chrome-extension,https
然后我可以了解相同的原产地政策概念.
然后,我从这里找到了许多相关的文章.
来自维基百科,
在计算中,同源策略是Web应用程序安全模型中的一个重要概念.根据该策略,Web浏览器允许第一个Web页面中包含的脚本访问第二个Web页面中的数据,但前提是两个Web页面具有相同的源.
但我有一个问题.
如果我使用<img src="abc.jpg">我可以加载图像.
为什么图像有例外?
我们如何加载图像,但我们无法加载其他文件类型?
我认为相同的原始政策应该应用所有文件类型,包括图像.
或者我是否错误地理解了同一个orgin-policy?
我正在尝试使用下面的代码获取IFrame内部HTML.
<iframe src="http://www.msn.com"
width="100%" height="100%" marginwidth="0"
scrolling="no" frameborder="0" id="divInfo"
onreadystatechange="MyFunction(this);"></iframe>
Run Code Online (Sandbox Code Playgroud)
JavaScript代码是
function MyFunction(frameObj)
{
if (frameObj.readyState == "complete")
{
alert(frameObj.document.body.innerHTML);
}
}
Run Code Online (Sandbox Code Playgroud)
但是警报显示了当前文档的html.当frmae就绪状态完成时,如何获得iframe的内部HTML.
如果我使用alert(frameObj.contentWindow.document.body.innerHTML);它给我访问被拒绝错误.
提前致谢.
我目前正在使用jQM开发移动Web应用程序.
虽然我找到了问题的答案,但我找不到任何好的答案.
这是我的目标:
由于跨域问题,我坚持第一步.
有些人写道如何使用带有回调函数的JSONP,但似乎该技术仅在处理JSON格式时有效.
我还读到,由于安全问题,JavaScript不支持在另一个域中下载html页面.
他们是对的吗?
没有办法通过JavaScript实现我的目标?
我试图为音频流创建一些可视化。但是,当我尝试使用createMediaElementSource()功能访问原始音频数据时,我遇到了CORS麻烦。
有没有办法避免这种限制,并从其他来源的流中获取原始音频数据?也许使用WebSockets?