weixin_33681778 2014-06-25 11:45 采纳率: 0%
浏览 8

Ajax仅同步工作

I have this code that sets my ajax request :

function sendAjaxRequest() {
            ajax.open("post", "form.php", false);
            ajax.setRequestHeader("Content-Type", "application/json");
            ajax.onreadystatechange = gotResponseFromServer();
            ajax.send(jsonObject);

        }

And this one php that executes the request :

    <?php
    require_once "database_connection.php";
    con = mysqli_connect($host, $user, $password,$db)  OR die("Failed to connect to MySQL: " . mysqli_connect_error());
    $data = file_get_contents("php://input");
    $decodedData = json_decode($data);


    //$verifyCode = md5(rand()."");
    $name = $decodedData->{'name'};
    $surname = $decodedData->{'surname'};
    $email = $decodedData->{'email'};
    $phone = $decodedData->{'phone'};
    $birthDate = $decodedData->{'birthDate'};
    $studies = $decodedData->{'studies'};
    $work = $decodedData->{'work'};
    $married = $decodedData->{'married'};
    $skills = $decodedData->{'skills'};
    $hobby = $decodedData->{'hobby'};
    $city = $decodedData->{'city'};
    $gender = $decodedData->{'gender'};
    $baptized = $decodedData->{'baptized'};
    $suggestions = $decodedData->{'suggestions'};
    $sql = "INSERT INTO Voluntari (_name, _surname, _email, _phone, _birthDate, ".
            "_studies, _work, _married, _skills, _hobby, _city, _gender, _baptized, _suggestions) ".
            "VALUES ('$name', '$surname', '$email', '$phone', '$birthDate', '$studies', '$work', '$married',".
            "'$skills', '$hobby', '$city', '$gender', '$baptized', '$suggestions'  );";

    if(!mysqli_query($con,$sql)) {
        die('Error: ' . mysqli_error($con));
    } else {
        //sendConfirmationMail();
        echo "added";
    }

    mysqli_close($con);
?>

The problem is that I can't make the ajax request asynchronously and I don't know why. If I do it synchronously, the form data gets added to the database but if I do in in an asynchronous way, the ajax.status is always 0 and ajax.readyState is 1.

To change the request from asynchronous to synchronous I put false in here :

ajax.open("post", "form.php", false);
                              ^^^^^

What am I doing wrong? And how can I keep my request asynchronous and make my script work?

  • 写回答

2条回答 默认 最新

  • weixin_33744854 2014-06-25 11:53
    关注

    If you look here for example: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp you will see that the parameters get described like this:

    open(method,url,async)
    method: the type of request: GET or POST
    url: the location of the file on the server
    async: true (asynchronous) or false (synchronous)

    To make your request asynchronous you will have to call ajax.open("post", "form.php", true);

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100