2014-12-09 87 views
0

嘗試從GET服務獲取響應時,ajax函數始終運行到錯誤部分。ajax從GET服務獲取JSON字符串失敗

<!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> 

當瀏覽器執行我得到這個結果代碼:

  • 數據:[對象對象]
  • textStatus:parserror
  • jqXHR:錯誤:jQuery1111 ..... 3111不叫

我試圖與數據類型: 「JSON」,但我得到這個錯誤在Chrome調試控制檯:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

和警報打印:

  • 數據:[對象的對象]
  • textStatus:錯誤
  • jqXHR:錯誤:

**** **** EDITED

我解決了它使用這個Chrome應用:https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

我鼓勵每個人都具有相同的問題,因爲我使用它。

+1

您使用的訪問此文件是什麼網址? – 2014-12-09 11:33:50

+0

僅嘗試將您的AJAX URL作爲'/ POC-WebApplication/obtenerusuarios'。 – Shubh 2014-12-09 12:05:59

回答

0

據我的理解,我認爲你必須將這些行添加到您.htaccess文件

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