[英]Jquery Rest Service response works only for Chrome browser but not in IE or FF
[英]Simple GET rest service works in IE only ( or Postman ) , but not in Chrome , not in Firefox
我有這個HTML / bootstrap代碼(實際上是button和“ demo” placer):
<button type="button" class="btn btn-primary"
onclick="displayAll()">Show/Refresh Storage list</button>
<br/>
<p id="demo"></p>
它調用這段JavaScript代碼:
function displayAll() {
var xmlhttp = new XMLHttpRequest();
var URL1 =
"http://localhost:8080/MyStore/rest/StoreService/discslist";
URL1 += "?dummy="+ Math.random();
xmlhttp.open("GET",URL1, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var discsList = JSON.parse(xmlhttp.responseText);
myFunction(discsList);
}
}
}
function myFunction(atr) {
var txt="";
txt += "<table border='1' class='table table-striped'>"
txt += "<thead><tr><th>id</th><th>Disc Name</th><th>Quantity</th>
<th>price</th></tr></thead>"
for (x in atr) {
txt += "<tr><td>" + atr[x].id + "</td>";
txt += "<td>" + atr[x].name + "</td>";
txt += "<td>" + atr[x].qnty + "</td>";
txt += "<td>" + atr[x].price + " $</td></tr>";
}
txt += "</table>"
document.getElementById("demo").innerHTML = txt;}
因此,主要思想是將Rest服務返回的數據顯示為表格,這在IE中很好(BTW,郵遞員也很好,我可以看到JSON輸出),但在Chrome和Firefox中則不能。
在我的調查中,我發現onreadystatechange事件在IE中觸發3次,僅在第三次觸發4和200時觸發。在Chrome中觸發一次,在Firefox中觸發兩次,但在兩種情況下均未從Tomcat發送回4/200。 (服務器是Tomcat 8.5。)
任何人都知道如何從這里開始,這可能是個問題嗎?
謝謝
好吧,我發現了問題。 它實際上與CORS(跨域資源共享)有關。 我剛剛在應用程序的web.xml中添加了CORS過濾器,並且效果很好。 這是方法: https : //enable-cors.org/server_tomcat.html
關於IE,為什么不能這樣工作:據我了解,這是因為我從桌面打開了html文件,因此IE在“ TRUST ZONE”中工作,因此對GET請求沒有應用CORS
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.