0

I'm trying to make a PHP connection, but I keep getting this error. I am hoping someone can help.

My code gives the following error:

{
    "readyState": 0,
    "status": 0,
    "statusText": "NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost/php/busca.php'."
}

My code is:

SendSQL.onclick = function() {

    var dataString='a=hello&b=world';

    $.ajax({
        type: "POST",
        url:"http://localhost/php/busca.php",
        data: dataString,
        async: false,
        dataType:'html',

        success: function(data){
                alert(data);
        },

        error: function(data){
            alert(JSON.stringify(data)); //Better diagnostics
        }
    });

};

And the file busca.php is:

<?php
    $a = $_POST['a'];
    $b = $_POST['b'];
    echo "$a, $b";
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

0

Try this approach...

SendSQL.onclick = function() {

  var dataString='a=hello&b=world';

  $.ajax({
    type: "POST",
    url:"http://localhost/php/busca.php",
    data: {
      "a":"hello",
      "b":"world"
    },
    async: false,
    dataType:'text',
    success: function(data){
            alert(data);
    },
    error: function(data){
        console.log(data); 
    }
  });

};
Carl Sare
  • 191
  • 1
  • 14
0

Your dataString is the way for sending parameter of GET request.

Let's change to JSON like

var dataString = { "a":"hello", "b":"world" };
$.ajax({
        type: "POST",
        url:"http://localhost/php/busca.php",
        data: {data: JSON.stringify(dataString)},
        async: false,
        dataType:'json',

        success: function(data){
                alert(data);
        },

        error: function(data){
            alert(JSON.stringify(data)); //Better diagnostics
        }
    });

And in php code, use json_decode($_POST['data'])

Toraaa
  • 145
  • 2
  • 10