user2932692
user2932692

Reputation: 3

can't access php with ajax json type

<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

Upvotes: 0

Views: 115

Answers (1)

Deep Mehta
Deep Mehta

Reputation: 1280

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');
            }
          }
                );

Upvotes: 1

Related Questions