javascript
  • php
  • jquery
  • mysql
  • ajax
  • 2017-02-23 103 views 1 likes 
    1

    我試圖發送數據到我的本地數據庫服務器,但當我嘗試發送它時,我總是收到400錯誤的請求錯誤。jquery Ajax返回400 BAD請求

    var studentEmail = "[email protected]"; 
          var dataString = '&questionNumber='+ temp + '&answer='+ value + '&email='+ studentEmail; 
    
    
          $.ajax({ 
          type: "POST", 
          dataType:'json', 
          url: "js/dbcon.php", 
          data: JSON.stringify(dataString), 
          processData: false, 
          contentType: "application/json; charset=utf-8" 
          }); 
    

    ,這是PHP文件

    <?php 
    $connection = mysql_connect("127.0.0.1", "root", "root"); // Establishing Connection with Server.. 
    $db = mysql_select_db("db", $connection); // Selecting Database 
    //Fetching Values from URL 
    $questionNumber=$_POST['questionNumber']; 
    $answer=$_POST['answer']; 
    $email=$_POST['email']; 
    //Insert query 
    $query = mysql_query("INSERT INTO answers (questionNumber,studentAnswer,studentEmail) VALUES ($questionNumber,$answer,$email)"); 
    echo "succesfully posted"; 
    mysql_close($connection); // Connection Closed 
    ?> 
    

    任何人都可以看到我在做什麼錯?

    在此先感謝!

    +0

    ps如果我更改POST以獲取它的作品,但不會將數據插入我的數據庫 – faradji

    +0

    您確定dbcon.php位於js文件夾中?似乎很奇怪...... – markvdlaan93

    +0

    您必須轉義字符串'@'在URL內無效。 – evolutionxbox

    回答

    0

    JSON.stringify()方法用於將javascript object轉換爲json字符串。

    所以dataString變量必須是一個javascript object

    var data ={questionNumber:temp ,answer: value ,email:studentEmail}; 
    

    AJAX

    $.ajax({ 
        type: "POST", 
        dataType:'json', 
        url: "js/dbcon.php", 
        data: JSON.stringify(data), 
        processData: false, 
        contentType: "application/json; charset=utf-8" 
    }); 
    
    +0

    謝謝你的回覆,我做了你所說的改變,現在我得到了404錯誤,我也確保php文件在js文件夾中 – faradji

    +0

    我認爲它找不到您的文件,但如果找到該文件,我的更改應該可以工作。 –

    +0

    @RiggsFolly,你說的是不正確的,因爲你已經使用過'contentType:「application/json; charset = utf-8」',它表示你發送的數據的類型。 –

    0

    如果你改變了後讓你不得不替換$ _ POST與$ _GET到PHP文件。

    0

    有通過將正常工作兩個POST數據和GET請求更簡單的方法

    var studentEmail = "[email protected]"; 
    $.ajax({ 
         type: "POST", 
         dataType:'json', 
         url: "js/dbcon.php", 
         data: {questionNumber:temp, answer:value, email: studentEmail}, 
         processData: false, 
    
    }); 
    

    現在的jQuery做所有的辛勤工作和轉換對象全到任何需要的格式數據POST或GET請求

    0

    您可以將ajax要求是這樣的:

    var studentEmail = "[email protected]"; 
          $.ajax({ 
          type: "POST", 
          dataType:'json', 
          url: "js/dbcon.php", 
          data: ({'questionNumber':temp,'answer':value, 'email':studentEmail }), 
          processData: false, 
          contentType: "application/json; charset=utf-8" 
          }); 
    

    而且PHP文件需要返回json字符串。

    echo "succesfully posted"; 
    

    是無效的json答案。

    返回是這樣的:

    $arr = array('success' => true, 'answer' => "succesfully posted"); 
    
    echo json_encode($arr); 
    

    參見這裏:http://php.net/manual/de/function.json-encode.php

    您應該驗證輸入數據,插入數據庫之前。

    相關問題