【问题标题】:jQuery AJAX Call to PHP Script with String Return带有字符串返回的 jQuery AJAX 调用 PHP 脚本
【发布时间】:2014-01-03 19:28:25
【问题描述】:

我的目标是在 PHP 中从 ASP.NET 复制 Hot Towel SPA 框架。

我的项目中有两个文件:show.js 和 displaystring.php。我基本上想要做的是返回 displayString() 函数中存在的任何内容。但是由于一系列的尝试都没有成功,我在shows.js文件中插入了debugger语句(如下源代码中所标注的)来找出问题所在。当我在 Google Chrome 中运行项目并打开开发人员工具(Ctrl + Shift + i)时,我看到程序流在调试器语句处停止(如预期的那样),并且当我将鼠标悬停在成功属性中的数据变量上时,我看到 displaystring.php 文件返回给我整个 html 源代码,而不是只返回 ".$_POST('string'); 语句。

我在互联网上浏览了许多解决方案,但无济于事。谁能告诉我我到底哪里错了?谢谢。

show.js

define(function (require) {

    var val = "";
    //var moviesRepository = require("repositories/moviesRepository");

    $.ajax({
      url: "controllers/displaystring.php",
      type: "post",
      data: {input:"show"},
      datatype: 'json',
      success: function(data){
            val = data;      
        debugger; // Debugger inserted here.
      }});

    return {
        result: ko.observable(),

        activate: function() {
            this.result(val);
        }
    };
});

显示字符串.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>String Display</title>
</head>

<body>
<?php
    function displayString() {
        return "The string passed is: ".$_POST('string');   
    }

    displayString();
?>
</body>
</html>

【问题讨论】:

  • 如果您将 $.ajax 数据类型设置为“json”,则您的响应必须是 json 编码的。 @imRcH 答案是正确的
  • 好的,如果我想传递并显示一个字符串值,那么代码应该是什么?
  • $.ajax{ 数据类型:"html",...
  • 我自己找到了问题的解决方案。我已经在答案列表中发布了解决方案。你可以在那里检查。无论如何,谢谢你的帮助。 :-)
  • 不客气@Razor

标签: javascript php jquery ajax hottowel


【解决方案1】:

试试这个方法

displaystring.php :

<?php
    function displayString() {
        return "The string passed is: ".$_POST['input'];   
    }
    echo displayString();
?>

【讨论】:

    【解决方案2】:

    首先你必须指定标题:

    header('Content-type: text/json');
    header('Content-type: application/json');
    

    其次,您必须为此类标题格式化您的内容:

    <?php
        function displayString() {
            return json_encode($_POST);   
        }
    
        echo displayString();
    ?>
    

    你也必须阅读official documentation

    1. Headers in php
    2. JSON format

    你的 php 文件必须是这样的:

    <?php
         header('Content-type: text/json');
         function displayString() {
                    return json_encode($_POST);   
                }
    
                echo displayString();
    

    【讨论】:

    • 非常感谢您的回复。我尝试了您的代码,但仍然无法正常工作。 :(
    • 尝试使用函数 displayString() { return json_encode($_POST); }
    • 我自己已经找到了解决问题的方法。我已将我的回复标记为帖子中的答案。非常感谢您的帮助。 :)
    【解决方案3】:

    好吧,在我的头撞了一整天的石墙上之后,我自己想出了解决方案。

    我只是稍微修改了代码,以便找到问题的答案。

    show.js

    define(function (require) { 
        return {
            result: ko.observable(),
    
            activate: function() {
                //var moviesRepository = require("repositories/moviesRepository");
                var self = this;
                $.ajax({
                    url: "controllers/displaystring.php",
                    type: "get",
                    data: {input:"show"},
                    dataType: "html",
                    success: function(data){
                        self.result(data);
                        debugger;
                    }});
            }
        };
    });
    

    显示字符串.php

    <?php
        function display() {
            echo 'Hi I am some random '.rand().' output from the server.';
        }
        display();
        //echo "Hello World!";
    ?>
    

    就是这样!您不必为 PHP 文件键入整个 HTML 代码。只需包含 PHP 脚本,您将获得输出。

    非常感谢大家的帮助。 :)

    【讨论】:

      【解决方案4】:

      只提到@tyagi 之类的 PHP 脚本并将 $_POST('string') 更改为 $_POST['string]。

      【讨论】:

      • 非常感谢您的回复。我尝试用方括号替换圆括号,但不幸的是它仍然无法正常工作。
      【解决方案5】:

      确保您没有在配置文件或构造函数或类似的东西中打印 html。我遇到了同样的问题。解决方案是创建单独的文件来处理 ajax 请求。

      【讨论】:

        猜你喜欢
        • 2013-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-11
        相关资源
        最近更新 更多