[英]Getting content from another page with javascript not working
我試圖對驗證captcha
執行客戶端驗證。 為此,我需要從外部url
獲取響應。 我使用了下面的javascript
代碼。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
function captcha_check()
{
var code = document.getElementById("captcha").value;
alert(code);
var url = "http://www.opencaptcha.com/validate.php?img='.$captcha_name.'.jpgx&ans="+code;
alert(url);
$.get(url,function(data,status){
alert(data);
if(data == "fail")
{
document.getElementById("captcha_error").style.display = "block";
return false;
}
else
{
return true;
}
});
}
</script>
但是代碼不起作用。 url
和code
正確提醒。 但是javascript
停止執行jquery函數以從url
獲取內容並跳過函數中的其余步驟。
請幫我找出我的代碼有什么問題。
謝謝大家。
除非響應接受來自所有域的請求,否則無法執行跨域ajax請求。 此外,我不認為驗證碼的客戶端驗證可以稱為“安全”。
更多信息: 跨源資源共享(CORS)
跨域ajax調用存在安全限制。
查看http://en.wikipedia.org/wiki/Same_origin_policy
我嘗試了從我的位置和Chrome返回的代碼:
XMLHttpRequest無法加載http://www.opencaptcha.com/validate.php?img=%27。$ captcha_name。%27.jpgx&ans = hi。 請求的資源上不存在“Access-Control-Allow-Origin”標頭。 因此,不允許來源“ http://fiddle.jshell.net ”訪問。
這是你期望的跨域調用。
這就是您的代碼失敗的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.