douchuntang2827 2014-01-01 16:07
浏览 71
已采纳

AJAX POST返回空值

I have tried and tested various methods for completing this task for about a day now. Please be forewarned that I am building this simply, and then working my way up!

I have a form that consists of a textarea, and two input fields. The input fields allow a XMLHttpRequest to send information pertaining to a username, and message - sent to a chatroom that I am trying to make.

The problem that I have with my request, is simply that I can send the information, and insert a row into a database, but I can't get any information back! You will see from the code below, that I have put an alert in, to check what the response text is, but it comes back as null (not undefined, but ""). Please check the code below:

function insertMessage() {
    var username = document.getElementById('username').value;
    var message = document.getElementById('message').value;
    var queryString = "username=" + username + "&message=" + message;

    // send the username and message information to be inserted into the database
    var url = 'classes/chatroom/chatroom.upload.php';

    // create xml request
    var request = createCORSRequest("POST", url)

    // create a function that will receive data sent from the server
    request.onreadystatechange = function () {
        if (request.readyState == 4 && request.status == 200) {
            alert(request.responseText);
        }
    }

    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.send(queryString);
}

function createRequest(method, url) {
    var thisRequest = new XMLHttpRequest();

    if ("withCredentials" in thisRequest) {
        // thisRequest has 'withCredentials' property only if it supports CORS
        thisRequest.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined") { // if IE use XDR
        thisRequest = new XDomainRequest();
        thisRequest.open(method, url);
    } else {
        thisRequest = null;
    }
    return thisRequest;
}

The code that pertains to the insertion of a database row is:

<?php
include 'chatroom.config.inc.php';  // the database file

$message_username = $_POST['username'];
$message_content = $_POST['message'];

if ($message_username == "Username: Once entered, you don't have to enter again" || $message_username == "") {
    $message_username = "Guest";
}

if ($message_content == "Message:" || $message_content == "") {}
else {
    $users->post_message($message_username, $message_content); // insert database row using PDO query
}
?>

Could anyone provide a clue as to where I'm going wrong?

  • 写回答

1条回答 默认 最新

  • dpruwm6206 2014-01-01 16:13
    关注

    The code looks good to me, your PHP code is inserting the data in DB but it isn't returning back any text or value.

    For values to be retrieved on the client side i.e. on successful completion of your ajax request, you will have to send the data to client side.

    Try using php's echo function and return the text / value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?