【问题标题】:Ajax calls a back end PHP script not workingAjax 调用后端 PHP 脚本不起作用
【发布时间】:2016-05-14 04:35:09
【问题描述】:

我尝试了这个SO 的答案。这不起作用。 test.php 确实给了我一个框来输入带有提交按钮的文本。 Firefox 浏览器开发者控制台在我提交之前给了我这条消息:

"SyntaxError: missing ) after argument list

未声明 HTML 文档的字符编码。如果文档包含 US-ASCII 范围之外的字符,则文档将在某些浏览器配置中呈现乱码。页面的字符编码必须在文档或传输协议中声明。”

在我输入文本并提交后:

“以 windows-1252 编码提交的表单无法编码所有 Unicode 字符,因此用户输入可能会损坏。为避免此问题,应更改页面以便以 UTF-8 编码提交表单通过将页面本身的编码更改为 UTF-8 或在表单元素上指定 accept-charset=utf-8。"

有什么建议吗?

test.php 有这个 sn-p:

<form action="javascript:void(0);" method="post">
    <input type="text" name="user" placeholder="enter a text" />
    <input type="submit" value="submit" />
</form>

<script type="text/javascript">
    $("form").submit(function(){
        var str = $(this).serialize();
        $.ajax('getResult.php', str, function(result){
            alert(result); // the result variable will contain any text echoed by getResult.php
        }
        return(false);
    });
</script>

getResult.php 有这个:

<?php
    echo "The name you typed is: " . $_REQUEST['user'];
?>

【问题讨论】:

  • 您是否检查过控制台以查看响应中发生了什么?在开发者工具中
  • @Harry:还有一个额外的错误:“SyntaxError: missing ) after argument list”
  • 我马上注意到您的脚本中缺少一个 )。
  • @user523234 那是你的问题

标签: javascript php html ajax


【解决方案1】:

首先,你在这里错过了一个右括号,

    $.ajax('getResult.php', str, function(result){
        alert(result); // the result variable will contain any text echoed by getResult.php
    }
     ^ missing );

其次,使用 POST 请求而不是 AJAX,如下所示:

$("form").submit(function(){
    var str = $(this).serialize();
    $.post('getResult.php', str, function(result){
        alert(result); 
    });
    return(false);
});

或者如果您想使用 AJAX 请求,请执行以下操作:

$("form").submit(function(){
    var str = $(this).serialize();
    $.ajax({
        type: 'POST',
        url: 'getResult.php',
        cache: 'false',
        data: str,

        success: function(result){
            alert(result)
        }
    });
    return(false);
});

【讨论】:

  • 如果我放 );根据您的建议,我遇到了一个新错误:ReferenceError: $ is not defined
  • @user523234 可能是你没有包含 jQuery 库。在您的脚本之前添加 jQuery 库,以便可以识别 $,如下所示:&lt;script src="http://code.jquery.com/jquery-1.11.0.min.js"&gt;&lt;/script&gt;
  • 这就是问题所在,因为我使用的是 ssl。 Firefox 也会阻止任何不使用 ssl 的调用。 code.jquery.... 将修复。谢谢!
【解决方案2】:

您应该在 getResult.php 代码中使用 $_POST['user'] 而不是 $_REQUEST['user'],如下所示:

<?php
    echo "The name you typed is: " . $_POST['user'];
?>

警告消息表明您没有使用适当的文本编码。您通过表单发送的某些字符不是“windows-1252”编码(例如表情符号)。 我建议您更改整个网站的编码,方法是在页面的 head 部分设置编码元标记,并删除您可能拥有的其他编码标记:

&lt;meta charset="UTF-8"&gt;

但是,如果您只想更改表单中的编码,我不建议这样做,请使用以下代码:

<form action="javascript:void(0);" method="post" accept-charset=utf-8>
    <input type="text" name="user" placeholder="enter a text" />
    <input type="submit" value="submit" />
</form>

<script type="text/javascript">
    $("form").submit(function(){
        var str = $(this).serialize();
        $.ajax('getResult.php', str, function(result){
            alert(result); // the result variable will contain any text echoed by getResult.php
        }
        return(false);
    });
</script>

【讨论】:

    猜你喜欢
    • 2014-03-08
    • 2013-07-02
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    相关资源
    最近更新 更多