这是两个页面,test.php和testserver.php.
test.php的
<script src="scripts/jq.js" type="text/javascript"></script>
<script>
$(function() {
$.ajax({url:"testserver.php",
success:function() {
alert("Success");
},
error:function() {
alert("Error");
},
dataType:"json",
type:"get"
}
)})
</script>
Run Code Online (Sandbox Code Playgroud)
testserver.php
<?php
$arr = array("element1",
"element2",
array("element31","element32"));
$arr['name'] = "response";
echo json_encode($arr);
?>
Run Code Online (Sandbox Code Playgroud)
现在我的问题是:当这两个文件都在同一台服务器(localhost或web服务器)上时,它可以工作并被alert("Success")调用; 如果它位于不同的服务器上,意味着Web服务器上的testserver.php和localhost上的test.php,它就无法工作,并且alert("Error")正在执行.即使ajax中的URL更改为http://domain.com/path/to/file/testserver.php
我想制作一个关于HTML/JS 同源政策的社区维基,希望能帮助任何人搜索这个主题.这是SO上搜索次数最多的主题之一,没有统一的wiki,所以我去:)
相同的源策略可防止从一个源加载的文档或脚本从另一个源获取或设置文档的属性.此政策可以追溯到Netscape Navigator 2.0.
请保持示例详细,最好还链接您的来源.
我想用jquery ajax用以下代码解析JSON数组数据:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sample</title>
<script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
var result;
function jsonparser1() {
$.ajax({
type: "GET",
url: "http://10.211.2.219:8080/SampleWebService/sample.do",
dataType: "jsonp",
success: function (xml) {
alert(xml.data[0].city);
result = xml.code;
document.myform.result1.value = result;
},
});
}
</script>
</head>
<body>
<p id="details"></p>
<form name="myform">
<input type="button" name="clickme" value="Click here to show the first name" onclick=jsonparser1() />
<input type="text" name="result1" readonly="true"/>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的JSON数据是:
{"Data": [{"Address":"chetpet","FirstName":"arulmani","Id":1,"LastName":"sathish","City":"chennai"},{"Address":"ramapuram","FirstName":"raj","Id":2,"LastName":"nagu","City":"chennai"},{"Address":"ramapuram","FirstName":"raj","Id":2,"LastName":"nagu","City":"chennai"},{"Address":"ramapuram","FirstName":"ramaraj","Id":3,"LastName":"rajesh","City":"chennai"},{"Address":"ramapuram","FirstName":"yendran","Id":3,"LastName":"sathi","City":"chennai"}],"Code":true}
Run Code Online (Sandbox Code Playgroud)
但我没有得到任何输出......任何人请帮助...
我使用YQL获取一些html页面来读取信息.从今天起,我收到了返回消息"不再支持html表.有关YQL使用条款,请参阅https://policies.yahoo.com/us/en/yahoo/terms/product-atos/yql/index.htm "
控制台中的示例:https://developer.yahoo.com/yql/console/#h=select+*+ from + html +其中+ url%3D%22http%3A%2F%2Fwww.google.de%22
雅虎是否停止了这项服务?有人知道雅虎的某种声明吗?我想知道这只是一个错误,还是他们真的停止了这项服务......
所有文档仍然存在(html抓取):https: //developer.yahoo.com/yql/guide/yql-select-xpath.html,https : //developer.yahoo.com/yql/
前一段时间我在雅虎的YQL论坛上发帖,现在这个不存在了(或者至少我找不到它).您如何联系雅虎以了解此服务是否真的停止了?
最好的问候,hebr3
我试图从我的localhost:4502端口将数据发布到API.当我尝试使用POSTMAN将数据发布到此API时,通过提供基本授权密钥将数据添加到后端.我试图在Ajax Jquery调用中实现这一点,但得到一个CORS错误.第一次在jquery我试图发布数据,请在这里帮助,我可以添加.我有基本授权的API密钥,因为用户名和密码可以留空.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#Save").click(function(){
var person = new Object();
person.Name = $('#Name').val();
person.EmailAddress = $('#EmailAddress').val();
person.CustomFields = [0];
person.CustomFields[0].Key = "[Country]";
person.CustomFields[0].Value = $('#Country').val();;
$.ajax({
url: 'https://api.createsend.com/api/v3.1/subscribers/7c7a6087b0e450ad72b38be83098e271.json',
type: 'POST',
dataType: 'json',
data:person,
success: function(data,textStatus,xhr){
console.log(data);
},
error: function(xhr,textStatus,errorThrown){
console.log('Error Something');
},
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic OTdlMjVmNWJiMTdjNzI2MzVjOGU3NjlhOTI3ZTA3M2Q5MWZmMTA3ZDM2YTZkOWE5Og==");
}
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud) 我正在尝试访问加载外部URL的iframe的DOM.当然,由于跨域安全性,我得到"权限被拒绝"错误.我怎样才能做到这一点?我看到用json完成了一些事情(但是我无法从外部源获取json字符串)以及使用HTML5 postmessage完成的事情.
你可以看到它:http: //jsfiddle.net/QPBvJ/
代码是:
$(document).ready(function(){
$('#get').live('click', function() {
var currentIFrame = $('#frameDemo');
currentIFrame.contents().find("a").css("background-color","#BADA55");
alert ("done")
});
});
<iframe src="http://api.jquery.com/" width="80%" height="600" id='frameDemo'></iframe>
<button id="get">Get</button>
Run Code Online (Sandbox Code Playgroud)
最简单的方法是什么?谢谢
我收到以下错误:jquery ajax readystate 0 responsetext status 0 statustext error给它时: url(http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm)但是当我url(localhost:""/embparse_page)在我的localhost上给它时,它工作正常.
我曾尝试使用我在Google搜索中找到的标题,我也使用beforeSend:""了它,但它仍然无效.
我认为主要问题是:XMLHttpRequest cannot load http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm. Origin "local server" is not allowed by Access-Control-Allow-Origin.但我不明白.
任何人都可以向我解释这个问题,因为我对此很陌生.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:ng="http://angularjs.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta Access-Control-Allow-Origin="*" />
<title>Page Parsing</title>
<script type="text/javascript" src="/js/jquery-1.9.1.min.js"></script>
<script>
getit=function(){
jQuery.support.cors = true;
$.ajax({
type:"GET",
url:"http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm",
dataType:"html",
crossDomain:true,
beforeSend: function(xhr) {
xhr.overrideMimeType('text/plain;charset=UTF-8');
},
success:function(XMLHttpRequest,jqXHR ,data) {
//alert(data.title);
var starttitl=data.lastIndexOf('<title>');
var endtitl=data.lastIndexOf('</title>'); …Run Code Online (Sandbox Code Playgroud) 我有一个http://网站需要访问在https://网站上公开的第三方JSON API .我已经阅读了通过方法来规避同源策略,但似乎那里描述的方法不适合我:
是这个吗?我必须实施解决方案4,这似乎相当复杂,或者我错过了什么?
我正在使用离子框架来开发本机应用程序.在这里,我在所有页面中都有默认标题.切换到第二页时,我需要应用内浏览器来查看外部内容.
所以,我使用了window.open
<a href="#" onclick="window.open('https://google.com','_blank','location=yes','closebuttoncaption=Return');">Click Here to view inapp browser</a>
Run Code Online (Sandbox Code Playgroud)
但是,当我在应用程序内浏览器中查看内容时,我需要标题保持不变.
在离子骨架中有可能吗?我不需要iframe.它在html中很重.
更新:
我有一个html文件,我注入iframe.喜欢
<div id="header"></div>
<iframe src="serveraddress/index.html"></iframe>
Run Code Online (Sandbox Code Playgroud)
而不是iframe,还有什么东西仍然保持标头不变?如果我使用应用内浏览器,我的标题是不可见的.
朋友今天问我这个问题,整天都在困扰着我.我已经找了几十个论坛寻找正确的方法获取外部html内容并在我的页面上显示它.
我想解决http://www.someExternalURL.com并从此页面检索所有html.我尝试了以下方法:
$.ajax
({
url: "http://www.someExternalURL.com",
type: "GET",
cache: false,
crossDomain: true,
data: {},
jsonp: 'jsonCallback',
dataType: "jsonp",
success: function (data) {
alert('good');
jsonCallback = data.Result;
},
error: function (e) {
alert(e.responseText);
}
});
Run Code Online (Sandbox Code Playgroud)
没工作.
然后我尝试了:
var all;
$.get("http://localhost:60939/About.aspx", function (my_var) {
alert(my_var);
}
Run Code Online (Sandbox Code Playgroud)
只有后者才适用于本地页面.我需要一个外部
任何帮助都会非常感激.
提前致谢
javascript ×7
ajax ×6
jquery ×6
cross-domain ×2
html ×2
json ×2
aem ×1
angularjs ×1
cors ×1
inappbrowser ×1
yql ×1