2012-10-07 65 views
-4

這是保存爲db2.php我的JSON,PHP不工作

<?php 

     mysql_connect("mysql15.000webhost.com","a3189966_root",""); 

     mysql_select_db("a3189966_pd"); 
     $firstName = $_GET[firstName]; 
     echo $firstName; 
     $q=mysql_query("SELECT * FROM people where first='$firstName'"); 

     while($e=mysql_fetch_assoc($q)) 
       $output[]=$e; 

      $json = json_encode($output); 
      echo($json); 

    mysql_close(); 

?> 

我的PHP代碼,這是我的HTML代碼與jQuery和JSON保存爲index2.html

$("#myForm").submit(function(){ 
        var postData = $(this).serialize(); 
        $.ajax({ 
         type: "GET", 
         dataType: "json", 
         data: $("#myForm").serialize(), 
         url: "db2.php", 
         success: function(data){ 
          alert("processing now" + data); 
            $.each(data,function(i,item){ 
                  alert("processing each data now"); 
             $('<li></li>').html(item.first + " " + item.last).appendTo('#data'); 
            }); 
         }, 
         error: function(data){alert("failed"+data);} 
        }); 
       return false; 
       }); 

    <form id="myForm" method="GET" action=""> 
    <input type="textbox" name="firstName" id="firstName"> 
    <input type="submit" name="myButton" value="submit"> 
</form> 
<ul id="data"> 
</ul> 

我後已經執行了下面的html代碼,它只會提示出錯。 我檢查了幾個小時,並且我看不到任何錯誤代碼被正確執行。 這是從PHP

[{"id":"1","first":"wen","last":"hao","age":"123"}] 
+0

我想這是因爲你不聲明'$輸出=陣列();'外的while循環。另外,爲什麼jsonp如果你不需要一個回調函數來傳遞來執行json? – Ohgodwhy

+0

請在將此代碼放入Web服務器之前更改此行:$ q = mysql_query(「SELECT * FROM people where first ='$ firstName'」);閱讀SQL注入和如何防止它 – iamkrillin

+0

[爲什麼$ foo \ [bar \]錯誤?](http://www.php.net/manual/en/language.types.array.php#language.types .array.foo-bar) – deceze

回答

1

ajax調用的錯誤回調可以帶三個參數。第二和第三個參數應該會讓你更好地瞭解發生了什麼問題。

jQuery ajax documentation

Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."

所以嘗試這樣的事情對你的Ajax調用:

$.ajax({ 
    type: "GET", 
    dataType: "json", 
    data: $("#myForm").serialize(), 
    url: "db2.php", 
    success: function(data){ 
     alert("processing now" + data); 
     $.each(data,function(i,item){ 
      alert("processing each data now"); 
      $('<li></li>').html(item.first + " " + item.last).appendTo('#data'); 
     }); 
    }, 
    error: function(jqXHR, textStatus, errorThrown) { 
     alert(textStatus); 
     if(errorThrown) { 
      alert(errorThrown); 
     } 
    } 
}); 
+0

我提交表單後,網址爲http://programmerpig.vacau.com/index2.php?firstName=wen&myButton=s提交 和2條提示提示 第一個提示是parseerror 第二條提示是語法錯誤:意外標記w – WenHao

+0

我已經刪除了php文件中的echo $ firstName之後,一切正常。〜我認爲錯誤是在意想不到的令牌語法錯誤w – WenHao

+0

是的,這是有道理的。有沒有理由在db2.php中打印$ firstName?你想發回的所有內容都應該在$ output中,因爲這是將數據編碼爲JSON – tomaroo

1
JSON對象返回

jsonp更改dataTypejson

dataType: "json", 
+0

其實之前,我有dataType:「json」,但它仍然無法正常工作.. – WenHao

+0

@ user970307你從控制檯得到什麼錯誤? – xdazz

+0

如果ajax失敗,則錯誤是錯誤部分中的消息。 – WenHao