【发布时间】:2014-04-14 10:22:40
【问题描述】:
我的 Angular 应用程序中有一个表单,用户需要在其中提供有效的 url。 因此,在提交表单时,我想通过向提供的 url 发出 get 请求并检查服务器是否发送 200 来测试该 URL 是否有效。
当点击提交按钮时,会运行以下代码:
$http.get(scope.target_url).success(function(){
// some code
}).error(function(error){
console.log(error);
});
但是,我从未得到成功的答案: - 如果我提供一个类似“http://www.somesite.com”的网址,我会得到:
XMLHttpRequest cannot load 'http://www.somesite.com'. Received an invalid response. Origin 'mydomain' is therefore not allowed access.
-
如果我提供类似“http://somesite.com”的网址,我会得到:
XMLHttpRequest 无法加载 http://somesite.com/。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'mydomain' 不允许访问。
问题出在哪里?
【问题讨论】:
-
这是因为same origin policy。您需要从服务器而不是客户端发出请求。
标签: javascript angularjs http get