让 AJAX 在 PhoneGap 中工作


Getting AJAX to work in PhoneGap

现在我需要使用 phonegap 调用另一个 Web 服务器上的 php 脚本来从我拥有的数据库中检索数据。使用硬代码运行脚本可以正确检索数据,但它显示为 ajax 调用没有返回任何内容,甚至没有将数据作为 post 获取。目前在控制台中,我正在"Origin null is not allowed by Access-Control-Allow-Origin. "

    $(document).ready(function() {      
       $('.check').click(function(){
       alert("Step1");
       var thisID    = $(this).attr('id');
       alert(thisID);
       $.ajax({
           type: "POST",
           url: "http://csmaster.sxu.edu/group2/group2/CougarLunch/retrieveColumn.php",
           data: { ID: thisID},
           cache: false,
           async:true,
           datatype: "json",
           success: function(data)
               {
             console.log(data);
             alert(data.ItemID);
              }
          });
          }); 
          });

PHP如果它是相关的,我怀疑它

    if(isset($_POST['ID']))
      {
       $ID = $_POST['ID'];
       function retrieve($ID)
         {
          $stmt = $mysqli->query("SELECT * FROM  group2.menu  WHERE ItemID = $ID");
          if($stmt->num_rows) //if there is an ID of this name
            {  
              $row = $stmt->fetch_assoc();
              echo $row;
              print json_encode($row);  
           }
        }

将此行添加到 php 文件的顶部以允许访问:

 header("Access-Control-Allow-Origin: "*");
你需要

在 Android Manifest 中传递.xml

<access origin="*" />

PhoneGap文档