douleijiang8111 2014-05-24 16:36
浏览 36

无法使用ajax json类型访问php

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.9.0.js"></script>
    <script charset="utf&minus;8" type="text/javascript">
        function connect(e)
        {
            var term= {button:e};
            $.ajax({
            url:'http://mydomain.com/android/reply.php',
            type:'POST',
            data:term,
            dataType:'json',
            error:function(jqXHR,text_status,strError){
            alert("status:"+text_status+" | error:"+strError);},
            timeout:60000,
            success:function(data){
            $("#result").html("");
            for(var i in data){
            $("#result").append("<li>"+data[i]+"</li>");
            }
            }
            });
        }
    </script>
</head>
<body>
    <center><b>Bikes or Cars</b></center>
    <center><input onclick="connect(this.value)" type="button" value="cars" /></center>
    <center><input onclick="connect(this.value)" type="button" value="bikes" /></center>
    <center><b>Results</b></center>
    <ul id="result"></ul>
</body>
 </html>

my php file:

 <?php
 header("Access-Control-Allow-Origin: *");
 $choice =$_POST["button"];
 $cars = array("Honde", "BMW" , "Ferrari");
 $bikes = array("Ducaite", "Royal Enfield" , "Harley Davidson");
 if($choice == "cars") print json_encode($cars);
 else print json_encode($bikes);
 ?>

problem is whenever i run this code from my localhost everything is fine but if i run this code in my android emulator or in my device i got an alert box says "status:error | error:forbidden" can someone explain this to me and how do i fix this. i am using phonegap 2.9

  • 写回答

1条回答 默认 最新

  • dsjxgu4759 2014-05-26 07:07
    关注

    Remove type:'POST' and try. i had the same problem and it got solved when i removed type:POST from ajax. below is my snippet of ajax post.

    $.ajax({
    
                dataType: "json",
                url: myurl,//link to ur webservice http://mydomain.com/android/reply.php
                data: sendForm,//data which you want to send
                success: function(data)
                {
                  alert('success');
                }
                ,
                error: function()
                {
                  alert('error');
                }
                ,
                complete: function()
                {
                  alert('completed');
                }
              }
                    );
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度