jQuery AJAX使用String返回调用PHP脚本

时间:2013-12-16 11:56:25

标签: javascript php jquery ajax hottowel

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

我的项目中有两个文件:show.js和displaystring.php。我基本上想要做的是返回displayString()函数中存在的内容。但是由于一系列不成功的尝试,我将调试器语句插入shows.js文件中(如下面的源代码所示),以找出错误。当我在谷歌浏览器中运行项目并打开开发人员工具(Ctrl + Shift + i)时,我看到程序流停在调试器语句处(正如预期的那样),当我将鼠标悬停在success属性中的数据变量上时,我看到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);
        }
    };
});

displaystring.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>

5 个答案:

答案 0 :(得分:2)

试试这种方式

displaystring.php :

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

答案 1 :(得分:1)

首先,您必须指定标题:

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
  3. 你的php文件必须是这样的:

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

答案 2 :(得分:1)

好吧,在我整天撞到石墙后,我自己想出了解决方案。

我刚刚修改了一下代码,以便找到问题的答案。

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

displaystring.php

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

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

非常感谢你的帮助。 :)

答案 3 :(得分:0)

只提供像@tyagi这样的PHP脚本&amp;将$ _POST('string')更改为$ _POST ['string]。

答案 4 :(得分:0)

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