【问题标题】:no response using JQuerys .post()使用 JQuerys .post() 没有响应
【发布时间】:2011-12-09 01:08:26
【问题描述】:

我有一个非常简单的表单来更新用户的状态消息。表单确实有效,但我没有得到任何响应(使用 firebug 提交后的响应为空)。

这里是 html/js:

<form id="StatusUpdateForm" method="post" action="http://www.url.com/process.php">
<input name="StatusUpdate" type="text" id="StatusUpdate" />
<input type="hidden" name="userid" value="<?=$_SESSION['user']['id']?>" />
<input type="submit" value="Update your status" />
</form>
<div id="results"></div>

<script>
/* attach a submit handler to the form */
$("#StatusUpdateForm").submit(function(event) {

/* stop form from submitting normally */
event.preventDefault(); 

/* get some values from elements on the page: */
var $form = $( this ),
StatusUpdate = $form.find( 'input[name="StatusUpdate"]' ).val(),
userid = $form.find( 'input[name="userid"]' ).val(),
url = $form.attr( 'action' );

/* Send the data using post and put the results in a div */
$.post( url , $("#StatusUpdateForm").serialize(),
     function( data ) {
          $('#results').html(data);
     }
);
});
</script>

这里是process.php:

if($_POST['StatusUpdate']&&$_POST['userid']){
    if( StatusMessage::set($_POST['userid'], $_POST['StatusUpdate']) ){
    echo "<div id='content'>Comment updated!</div>";
    } else {
    echo "<div id='content'>Problem updating comment. Try again.</div>";
    }
}

【问题讨论】:

  • 由于您期望返回 HTML,因此您应该只返回 HTML。只有 JSON 请求应该是 JSON 编码的。
  • PHP 是否在您的 httpd.log 中抛出任何错误?你有没有试过用总是返回的东西替换 php 代码,以确保你的脚本得到了 ajax 回复?
  • 使用 Fiddler 检查响应,看看它是否确实为空或只是未能插入。
  • @Kenaniah 我试过只返回 HTML,也改变了它。不过还是不行。
  • 另外,我对 PHP 不是很了解,但是顶级范围内的 return 是否会发送内容来响应?不应该是echo什么的吗?

标签: php jquery ajax


【解决方案1】:

return 更改为echo。如果它没有输出任何东西,就不会有任何响应发送!

if ($_POST['StatusUpdate'] && $_POST['userid']) {
    if (StatusMessage::set($_POST['userid'], $_POST['StatusUpdate'])) {
        echo "<div id='content'>Comment updated!</div>";
    } else {
        echo "<div id='content'>Problem updating comment. Try again.</div>";
    }
} else {
    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) 
        && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
    ) {
        echo "<div id='content'>Some other error!</div>";
    }
}

为了更加确定它正在执行,添加一个错误回调函数:

$.post(url , $("#StatusUpdateForm").serialize(), function(data) {
    $('#results').html(data);
}).error(function(eventData) { 
    alert('error: ' + eventData); // try that
});

或(首选):

$.ajax({
    type: 'POST',
    url: url,
    success: function(response) {
        $('#results').html(response);
    },
    error: function(xhr, textStatus, errorThrown){
        // alert(errorThrown);
        alert(textStatus);
    }
});

【讨论】:

  • 我试过了,不行。不过,我已经更新了问题。
  • 我添加了错误回调,但我收到了一个错误。但表单确实执行了,因为它有效...我只是没有得到响应 html。
  • 风格说明:如果你要使用延迟模式,那么你应该对失败和完成事件都这样做。
  • @mmmshuddup 我认为 evenData 是一个数组......这就是它所说的“错误:[object Object]”
  • 然后尝试alert(xhr.statusText);
【解决方案2】:

在调用 ajax 时不能使用绝对路径。 即使没有调用外部网址

http://www.url.com/process.php 更改为process.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 2019-01-23
    相关资源
    最近更新 更多