Pete_ch
Pete_ch

Reputation: 1321

jQuery ajax call to php giving a 404 not found: what am I missing?

I have a jQuery Ajax call to php giving a 404 not found. What am I missing? Requesting the page (non Ajax) from a browser gives me the json data back.

The jQuery calling the php by Ajax:

$.post({
    url:'/jqgrid/nwproducts.php',

    success:function(data){
        $('#auto').autocomplete({source:data.ProductName});
    }
});

The php code:

    <?php
header("Content-Type: application/json"); 
header("HTTP/1.1 200 OK");

$arrayProduct = array();
$mysqli =  new mysqli('localhost','login','passwd','northwind');
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

$resultAll = $mysqli->query('select ProductName from products');

if (!$resultAll)
{
    echo "error\n";
} else {
    while ($obj = $resultAll->fetch_object()) {
    array_push($arrayProduct,$obj);
    }


echo json_encode($arrayProduct);
}
?>

I am getting the following error in firebug:

http://localhost/jqgrid/%5Bobject%20Object%5D 404 Not Found

Upvotes: 2

Views: 10160

Answers (3)

Pranay Rana
Pranay Rana

Reputation: 176906

If possible, check whether the page is accessible and whether you provided the correct page URL.

One more solution: if possible make use of an Ajax function that gives you more flexibility.

 function IsExists(pagePath, dataString) {
         //alert(pagePath);

         $.ajax({
             type: "POST",
             url: pagePath,
             data: dataString,
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             error: function(XMLHttpRequest, textStatus, errorThrown) {
                 alert(textStatus);

             },
             success:
                                function(result) {
                                    var flg = true;
                                    if (result != null) {

                                        flg = result.d;

                                        if (flg == "True") {
                   alert('Success-true');                                            }
                                        else {
                 alert('Success - false');                                            }
                                    }
                                }
         });

     }

   

Upvotes: 0

Pete_ch
Pete_ch

Reputation: 1321

Hi Pranay I used your code modified a little bit but it worked

(function IsExists(pagePath) {
         $.ajax({
             type: "POST",
             url: pagePath,
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             error: function(XMLHttpRequest, textStatus, errorThrown) {
                 alert(textStatus);
             },
             success:function(result) {
                             $('#auto').autocomplete({source:result});
                                }
         });
     })('/jqgrid/nwproductsonly.php');

Upvotes: 1

YuS
YuS

Reputation: 2045

Put out url and success param off the object.

$.post('/jqgrid/nwproducts.php', function(data) {
  $('#auto').autocomplete({source:data.ProductName});
});

http://api.jquery.com/jQuery.post/

Upvotes: 1

Related Questions