简体   繁体   English

jQuery PHP $ .post()服务器响应将不起作用

[英]JQuery PHP $.post() server response will not work

I'm trying to figure out how to get a response back from my $.post() and I can't seem to figure it out. 我试图弄清楚如何从$ .post()返回响应,但似乎无法弄清楚。 It works just fine in IE but chokes in FF and Chrome. 它在IE中工作正常,但在FF和Chrome中令人窒息。 I am sure it's something really simple. 我相信这确实很简单。 I have read through as many postings here and on Google trying all the suggestions, still no luck. 我在这里和Google上阅读了尽可能多的帖子,尝试了所有建议,但还是没有运气。 Can someone see what I am doing wrong? 有人可以看到我在做什么吗? The response I should expect is testing . 我应该期望的响应是testing

Forgot to include what I am getting; 忘记包括我得到的东西; in the Chrome and FF Firebug console I see the .error() message and all it has is 'error' ... no description. 在Chrome和FF Firebug控制台中,我看到了.error()消息,它只有'错误'...没有描述。

Library 图书馆

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

HTML 的HTML

<form id="frm">
    <input type="text" name="usr" />
    <input type="submit" value="Click" />
</form>

JQuery jQuery查询

$(function()
{
    $('form#frm').submit(function()
    {
        $.post('process.php',$('#frm').serialize(),function(response)
        {
            console.log('PHP returned : '+response);
        })
        .error(function(XMLHttpRequest,textStatus,errorThrown)
        {
            console.log('Error with ajax Function: '+ textStatus+' '+errorThrown);
        });
    });
});

PHP 的PHP

<?php
print('testing');
?>

EDIT (For anyone else that's having the same issue, this code works) 编辑 (对于其他有相同问题的人,此代码有效)

$(function()
{
    $('form#frm').submit(function(e)
    {
        e.preventDefault();
        $.post('process.php',$('#frm').serialize(),function(response)
        {
            /* Do something with response */
        })
        .error(function(XMLHttpRequest,textStatus,errorThrown)
        {
            alert('Error with ajax Function: '+ textStatus+' '+errorThrown);
        });
    });
});

I assume that since your you did not specify a form action and don't prevent the form from submitting by returning false in the event handler, the page is being refreshed (you post to the same page and it simply reloads) before the AJAX callback is executed. 我假设由于您没有指定form action并且没有通过在事件处理程序中返回false来阻止表单提交,因此在AJAX回调之前将刷新页面(将您发布到同一页面并重新加载)被执行。 That would not explain the error message though. 但这不能解释错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM