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

时间:2022-05-13 04:48:27

My aim here is to replicate the Hot Towel SPA framework from ASP.NET in PHP.

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

I have two files in my project: show.js and displaystring.php. What I basically want to do is return whatever is present in the displayString() function. But due to a series of unsuccessful attempts, I inserted the debugger statement in the shows.js file (as marked in the source code below) to find out what's wrong. When I run the project in Google Chrome and open the developer tools (Ctrl + Shift + i), I see that the program flow stops at the debugger statement (as expected) and when I hover my mouse over the data variable in the success property, I see that the displaystring.php file returns me the entire html source code instead of just returning the ".$_POST('string'); statement.

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

I have gone through many solutions on the internet but to no avail. Can anyone please tell me where exactly am I going wrong? Thank you.

我在互联网上经历了很多解决方案,但无济于事。谁能告诉我我哪里错了?谢谢。

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 个解决方案

#1


2  

Try this way

试试这种方式

displaystring.php :

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

#2


1  

First of all you must specify headers:

首先,您必须指定标题:

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

Second you must to formmat your content for this kind of headers:

其次,你必须为这种标题形成你的内容:

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

    echo displayString();
?>

And also you must to read official documentation:

您还必须阅读官方文档:

  1. Headers in php
  2. php中的标题

  3. JSON format

You php file must be something like that:

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

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

            echo displayString();

#3


1  

Okay after banging my head against a stone wall for the whole day, I figured out the solution myself.

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

I just modified the code a bit to serve the purpose of finding the answer to my question.

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

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!";
?>

And that's it! You don't have to type the entire HTML code for the PHP file. Just include the PHP script and you will get the output.

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

Thank you all very much for your help. :)

非常感谢你的帮助。 :)

#4


0  

Have only PHP script like @tyagi mentioned & change $_POST('string') to $_POST['string].

只有像@tyagi这样的PHP脚本提到并将$ _POST('string')更改为$ _POST ['string]。

#5


0  

Make Sure You didn't print the html in config file or constructor or something like that. I got the the same problem. Solution is to create separate file to handle the ajax request.

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

#1


2  

Try this way

试试这种方式

displaystring.php :

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

#2


1  

First of all you must specify headers:

首先,您必须指定标题:

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

Second you must to formmat your content for this kind of headers:

其次,你必须为这种标题形成你的内容:

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

    echo displayString();
?>

And also you must to read official documentation:

您还必须阅读官方文档:

  1. Headers in php
  2. php中的标题

  3. JSON format

You php file must be something like that:

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

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

            echo displayString();

#3


1  

Okay after banging my head against a stone wall for the whole day, I figured out the solution myself.

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

I just modified the code a bit to serve the purpose of finding the answer to my question.

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

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!";
?>

And that's it! You don't have to type the entire HTML code for the PHP file. Just include the PHP script and you will get the output.

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

Thank you all very much for your help. :)

非常感谢你的帮助。 :)

#4


0  

Have only PHP script like @tyagi mentioned & change $_POST('string') to $_POST['string].

只有像@tyagi这样的PHP脚本提到并将$ _POST('string')更改为$ _POST ['string]。

#5


0  

Make Sure You didn't print the html in config file or constructor or something like that. I got the the same problem. Solution is to create separate file to handle the ajax request.

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