jQuery验证来自另一个域的url不起作用 [英] JQuery to validate url from another domain not working
问题描述
我是JQuery的新手.我的要求是验证来自另一个域的链接.如果成功,则重定向到该链接(在新页面中打开页面),否则显示警报.
I am kind of new to JQuery. My requirement is to validate a link from another domain. If success, then redirect (Open the page a new window) to that link, otherwise show alert.
我在Jsfiddle中尝试过的内容如下:
What I tried in Jsfiddle are given below:
$.ajax({
url: "/user/login",
method: 'head',
error: function(){
alert('Failure');
},
success: function(){
alert("Success");
}
})
以上代码成功验证了该URL.但是,一旦我将网址更改为 http://www.google.com ,它就无法正常工作.代码段如下:
The above one succesfully validated the URL. But once I changed the url to http://www.google.com, it is not working. Code snippet is given below:
$.ajax({
url: "http://google.com",
method: 'head',
error: function(){
alert('Failure');
},
success: function(){
alert("Success");
}
})
有人知道为什么这行不通吗,有什么办法可以解决这个问题?我刚刚发现JQuery不支持跨域验证.是真的吗?
Any idea why this is not working and is there any way to solve that? I just found out that cross domain validation is not supported in JQuery. Is it true?
推荐答案
<script>
var testUrl = "http://www.google1.com";
$.ajax({
url: testUrl,
dataType: 'jsonp',
crossDomain: true,
timeout: 5000,
complete: function( e, xhr, settings ) {
switch( e.status ) {
case 200:
window.open( testUrl );
break;
default:
alert( 'Not Valid' );
return false;
break;
}
}
});
</script>
或者,我将向内部服务器端脚本发送AJAX请求,然后使用某些服务器端方法进行检查,例如PHP的cURL
/get_file_contents()
.
Alternatively, I would send a AJAX request to an internal Server Side Script would then check using some server side methods, such as cURL
/get_file_contents()
for PHP.
这篇关于jQuery验证来自另一个域的url不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!