2013-12-16 49 views
3

我的目标是在PHP中复制ASP.NET中的Hot Towel SPA框架。jQuery AJAX通过字符串返回调用PHP脚本

我的项目中有两个文件:show.js和displaystring.php。我基本上想要做的是返回displayString()函数中存在的任何东西。但是由于一系列不成功的尝试,我在show.js文件中插入了调试器语句(如下面的源代码所示)以找出错误。当我在谷歌浏览器中运行该项目并打开开发人员工具时(Ctrl + Shift + i),我发现程序流停止在调试器语句处(如预期的那样),并将鼠标悬停在success属性中的数据变量上,我发现displaytring.php文件返回的是整个html源代码,而不是仅返回“。$ _ POST('string');语句”。012 _ _ _ _ _ _ _ _ _ 。谁能告诉我哪儿我该怎么错了?谢谢。

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.ph p

<!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> 
+0

如果您将$ .ajax数据类型设置为'json',那么您的响应必须是json编码。 @imRcH答案是正确的 – vrunoa

+0

好吧,如果我想传递并显示一个字符串值,那么代码应该是什么? – Razor

+0

$ .ajax {datatype:“html”,... – vrunoa

回答

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脚本,您将获得输出。

非常感谢你的帮助。:)

2

尝试这种方式

displaystring.php : 

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

只有PHP脚本像@tyagi提到&改变$ _ POST( '字符串'),以$ _ POST ['字符串。

+0

非常感谢您的回复。我试图通过用正方形替换圆括号,但不幸的是它仍然无法正常工作。 – Razor

1

首先,你必须指定标题:

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

第二你一定要formmat内容对于这种头:

<?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(); 
+0

非常感谢您的回复。我试过你的代码,但它仍然无法工作。 :( – Razor

+0

安置自己的错误请 – RDK

+0

我没有得到任何错误,但有一个空白页 – Razor

0

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

相关问题