【发布时间】:2015-02-07 06:50:15
【问题描述】:
试图从 GET 服务获取响应时,ajax 函数总是遇到错误部分。
<!DOCTYPE html>
<!-- jquery -->
<link rel="stylesheet" href="jquery/jquery-ui.css">
<script src="jquery/jquery-1.11.1.min.js"></script>
<script src="jquery/jquery-ui.js"></script>
<!-- bootstrap -->
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css">
<script src="bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript">
$.ajax(
{
url: "http://localhost:8080/POC-WebApplication/obtenerusuarios",
type: "GET",
dataType: "jsonp",
success: function(data, textStatus, jqXHR)
{
alert("success");
},
error: function(data, textStatus, jqXHR)
{
alert("FAIL!!!!\ndata: "+data+"\ntextStatus: "+textStatus+"\njqXHR: "+jqXHR);
}
});
</script>
当浏览器执行代码时,我得到了这个结果:
- 数据:[对象对象]
- 文本状态:解析错误
- jqXHR: 错误: jQuery1111.....3111 未被调用
我尝试使用 dataType: "json" 但在 chrome 调试控制台中出现此错误:
请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin 'null'。
警报打印:
- 数据:[对象对象]
- 文本状态:错误
- jqXHR:错误:
****已编辑****
我使用这个 chrome 应用解决了这个问题:https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi
我鼓励和我有同样问题的每个人都使用它。
【问题讨论】:
-
您使用什么 URL 访问此文档?
-
仅以
/POC-WebApplication/obtenerusuarios的身份尝试您的 AJAX 网址。
标签: javascript jquery ajax