2012-01-20 32 views
2

我有一個jQuery AJAX調用PHP給404未找到。我錯過了什麼? 從瀏覽器請求頁面(非ajax)給我返回json數據。jQuery ajax調用PHP給404未找到 - 我在想什麼?

jQuery的調用由AJAX的PHP:

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

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

PHP代碼:

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

我收到以下錯誤螢火蟲:

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

井,404意味着沒有頁面在你的'/的jqGrid/nwproducts.php'網址發現...我會開始調查,如果我可以從那裏實際訪問我的PHP文件。 –

+0

「/ jqgrid/nwproducts.php」的完整URL和你稱之爲ajax的URL是什麼?也許base不會正確地放置絕對路徑,導致404不能是PHP錯誤,它是不正確的URL。跟蹤ajax真正調用的URL。 – YuS

+0

url正確瀏覽給它一個json對象數組;不知道%urlobject%20Object在URL中的錯誤返回意味着什麼 –

回答

1

嗨Pranay我用你的代碼修改一點點,但它的工作

(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'); 
1

掐掉urlsuccess param off the object。

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

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

0

如果可能的話檢查頁面是易觸及和頁面的烏爾提供正確的URL。

一個更slution如果可能的話化妝使用了Ajax Fuctnion這使得更多flexiblity給你的。

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

    }