weixin_33716941 2011-12-09 01:08 采纳率: 0%
浏览 193

使用JQuery .post()没有响应

I have a very simple form for updating a user's status message. The form does work, but I am not getting any response (using firebug the response after submit is empty).

Here is the 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>

Here is 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>";
    }
}
  • 写回答

2条回答 默认 最新

  • larry*wei 2011-12-09 01:57
    关注

    Change return to echo. If it's not outputting anything there won't be any response to send!

    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>";
        }
    }
    

    To be extra sure it's executing, add an error callback function:

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

    Or (preferred):

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器