【问题标题】:ajax fails obtaining the JSON string from GET service [duplicate]ajax 无法从 GET 服务获取 JSON 字符串 [重复]
【发布时间】: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


【解决方案1】:

据我了解,我认为您必须将这些行添加到您 .htaccess 文件中

<IfModule mod_headers.c>
   Header set Access-Control-Allow-Origin "*"
</IfModule>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    相关资源
    最近更新 更多