得到json没有'Access-Control-Allow-Origin'错误 - 电话缺口

时间:2016-01-19 17:29:28

标签: javascript php jquery json cordova

在尝试检索和显示我的phonegap应用程序中的信息时,我一直收到此错误消息。

这是我得到的错误:

获取http://192.168.0.4:3000/Application/www/retrieveSymbol.php发送@jquery-2.2.0.js:9172jQuery.extend.ajax @jquery-2.2.0.js:8653jQuery。(匿名函数)@jquery-2.2.0.js:8804jQuery。 extend.getJSON @jquery-2.2.0.js:8785(匿名函数)@ bookmarks.html:44 bookmarks.html:1 XMLHttpRequest无法加载http://192.168.0.4:3000/Application/www/retrieveSymbol.php。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许原点'null'访问。响应的HTTP状态代码为404.

这是代码:

<script>
//populates product container
$(" #productContainer").html('');
$.getJSON("http://192.168.0.4:3000/Application/www/retrieveSymbol.php", function(data){  //retrieves json array
  $.each(data, function(i, field){          //loops through array
    $("#productContainer").append(          //creates product box filling it with data
      "<div id='productBox'>" + "<div id='name'>" + field[1] + " </div>" + "<br>" +
      "<div id='image'>" + "<img id='image' src='Images/" + field[5] + "'/>" + "</div>" +
      "<div id='description'>" + "<b>Description:</b> "  + "<br>" + field[2]  + " </div>" + "" +
      "<div id='price'>" + "£" + field[3] + " </div>" + "<br><br>" +  "<br>" +

      //gets name and price and sends data to basket when button is clicked
      "<form action='Basket.php' method='post' id='add'>" +
      "<input type='hidden' name='itemName' value=" + field[1] + "/>" +
      "<input type='hidden' name='itemPrice' value=" + field[3] + ">" +
      "<input id='addButton'type='submit' value='Add to Cart'>" +
      "</form>" + "</div>"

    );
  });
});
</script>

recieveSymbol.php

<?php

//connect to the database
$mysqli = NEW MySQLi ('localhost','root','','contentdatabase');
//query database
$resultSet = $mysqli->query("SELECT * FROM items");
//count the rows
if($resultSet->num_rows != 0) {
  //turn the results into an array
  $rows = $resultSet->fetch_all();
  echo json_encode($rows);

}else{
  echo "{no connection}";
}
?>

任何人都可以指出为什么会这样吗?

2 个答案:

答案 0 :(得分:0)

这是典型的跨源错误,可以在大多数PhoneGap项目中看到。请查看此答案以获取更多详细信息和解决方案。

No 'Access-Control-Allow-Origin' in PhoneGap/Cordova, as well as the latest versions of Google Chrome

答案 1 :(得分:0)

使用此Chrome扩展程序Allow-Control-Allow-Origin,它允许您从任何来源请求任何包含ajax的网站