1 Reply Latest reply: Jan 8, 2014 12:34 PM by Mikael Gerhardsson RSS

    ajax url to php file

    Mikael Gerhardsson

      Is there anyone with experience of using ajax to call php file from an extension object? I wrote some code yesterday that works on my local web server but when I try to use the same javascript code in extension object I am having trouble with the ajax url since it needs to be executed on a web server.

       

      Works in web browser on my local server.

       

      $.ajax({
            url: "test.php",
            data: "",
            datatype: "json",
            success: function(resp){
              var result = JSON.parse(resp);
              $("#result").empty();
              
              for(i=0; i<result.length; i++){
                $('#result').append("<p>"+result[i].FirstName 
                  + " " + result[i].LastName+"</p>");
              }
              
            },
            error: function(e){
              alert("error: "+e);
            }
          });
      

       

      My questions is how I should specify the url when calling from an extension object... I have tried:

       

       $.ajax({
            url: "http://127.0.0.1:8080/test.php",
            data: "",
            datatype: "json",
            success: function(resp){
                 alert("success");
            },
      
            error: function(e){
                 alert("error");
            }
          });
      

       

      but will only return error. I can access the specified url from my browser fine. I have also tried to put it on an external web server with same result.

       

      php script I am using:

       

      <?php 
        function connect(){
        $data = array();
      
        $link = mysqli_connect("localhost","****","******" , "test") or die("Error " . mysqli_error($link));
        $strSQL = "SELECT FirstName, LastName FROM test";
      
        $query = mysqli_query($link, $strSQL);
        while($result = mysqli_fetch_array($query)){
        $data[] = array(
        'FirstName' => $result["FirstName"],
        'LastName' => $result["LastName"]
        );
        }
        mysqli_close($link);
        echo json_encode($data);
        }
      
      connect();
      ?>
      

       

      Should I use ajax GET? Any help is welcome.

        • Re: ajax url to php file
          Mikael Gerhardsson

          Solved it by using:

           

            jQuery.support.cors = true;
          

           

          Full code:

           

           jQuery.support.cors = true;
            $.ajax({
                type: "GET",
                url: "http://127.0.0.1:8080/test.php",
                data: "",
                datatype: "json",
                success: function(resp){
                  alert("success: "+resp);
                  var result = JSON.parse(resp);
                  $("#result").empty();
                  
                  for(i=0; i<result.length; i++){
                    $('#result').append("<p>"+result[i].FirstName 
                      + " " + result[i].LastName+"</p>");
                  }
                  
                },
                error: function(e){
                  alert("fail");
                }
              });
          

           

          /M