这是两个页面,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
我正在尝试使用AJAX加载跨域HTML页面,但除非dataType是"jsonp",否则我无法获得响应.但是,使用jsonp,浏览器需要一个脚本mime类型,但是正在接收"text/html".
我的请求代码是:
$.ajax({
type: "GET",
url: "http://saskatchewan.univ-ubs.fr:8080/SASStoredProcess/do?_username=DARTIES3-2012&_password=P@ssw0rd&_program=%2FUtilisateurs%2FDARTIES3-2012%2FMon+dossier%2Fanalyse_dc&annee=2012&ind=V&_action=execute",
dataType: "jsonp",
}).success( function( data ) {
$( 'div.ajax-field' ).html( data );
});
Run Code Online (Sandbox Code Playgroud)
有没有办法避免使用jsonp进行请求?我已经尝试过使用crossDomain参数,但它没有用.
如果没有,是否有任何方式在jsonp中接收html内容?目前,控制台在jsonp回复中说"意外<".
我正在制作ajax jsonp请求,但失败错误处理不起作用.如果请求是404或500,它将无法处理错误.
我一直在寻找答案,但找不到任何答案.http://code.google.com/p/jquery-jsonp/似乎有一个解决方案,但我找不到任何关于如何使用它的示例.
function authenticate(user, pass) {
$.ajax ({
type: "POST",
url: "url",
dataType: 'jsonp',
async: false,
//json object to sent to the authentication url
data: {"u": userid, "p": pass},
success: function (data) {
//successful authentication here
console.log(data);
},
error: function(XHR, textStatus, errorThrown) {
alert("error: " + textStatus);
alert("error: " + errorThrown);
}
})
}
Run Code Online (Sandbox Code Playgroud) 如您所知,Web浏览器的安全性不允许发出跨域请求.我读了一本书,说只有在你可以将文件放在服务器上时才应该使用XMLHTTPRequest(意味着你要加载到同一个请求域的页面).如果你不能 - 你应该寻找替代方案.
我的问题是:
编辑: 我还不清楚......
例如,我从www.domain1.com拉取我的页面,我需要从www.domain2.com请求javascript .因此,拉出的页面应包含以下内容:
<script src="www.domain2.com/script.js"></script>
Run Code Online (Sandbox Code Playgroud)
避免跨域限制.
我可以使用JSONP,请求看起来像:http://ww.domain1.com/?callback = someFunction.js
但是:是不是一样?我只是从另一个域中拉js!它是否避免跨域限制?
我无法使用jQuery.support.cors = true行从下面的代码中打印成功; .包括行jQuery.support.cors = true; 会发出警告信息.那么如何在不失去功能的情况下避免这种情况呢?我的主要目标是调用一个返回JSON数据的rest webservice,我必须使用JSON数据.请帮助我如何实现这一目标?请提供一份工作样本
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.getJSON demo</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<script>
jQuery.support.cors = true;
$.ajax ({
url: 'http://json-cricket.appspot.com/score.json',
datatype: "json",
success: function (e) {
// Success callback
alert("sucess");
}})
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我有两个项目,一个是WCF服务,它在文本框中说出文本/句子.
public class Service1 : IService1
{
public string RunTts(string text)
{
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
// Configure the audio output.
synth.SetOutputToDefaultAudioDevice();
synth.Speak(text);
return "";
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在第二个项目的_Layout.cshtml页面中用ajax调用它,它是asp.net mvc.
<script type="text/javascript">
function ttsFunction() {
serviceUrl = "Service1.svc/RunTts";
$.ajax({
type: "POST",
url: serviceUrl,
data: '{"text": "' + $('#speak').val() + '"}',
contentType: "text/xml; charset=utf-8",
dataType: "text/xml",
error: function (xhr,status,error) {
console.log("Status: " + status); // got "error"
console.log("Error: " + error); // got "Not Found"
console.log("xhr: " + …Run Code Online (Sandbox Code Playgroud) 我正在处理第三方Web服务,我必须在其中发出一个包含JSON的POST请求.但是如何处理jQuery?
每次我想要POST时,都会收到"Cross-Origin not allowed"这样的错误.我看了,这是正常的,但我找不到有效的解决方案.所以我无法推荐我的客户"嘿,请用特殊标志启动浏览器".另一方面,我无法在服务器端进行调整.但是向其他资源发出POST请求应该不是什么大问题!?