php - 原型(prototype) Javascript 框架 - 获取 PHP 响应

标签 php javascript response prototypejs

所以我正在使用 Prototype JavaScript 框架,我别无选择,因此不幸的是其他框架/库也无济于事。

简而言之,我使用 Prototype.js 向 PHP 页面发送请求,如下所示:

<input type="button" value="submit by AJAX" onclick="sendRequest();" />
<script>
        function sendRequest(){
            var url = "a_php_page.php";
            var pars = "param={\"some JSON parameters\"}";
            //alert(pars);
            var codeAjax = new Ajax.Request(url, {
                method :'post',
                parameters :pars,
                asynchronous :true,
                onSuccess : function(result){
                    document.write(JSON.stringify(result.responseText));
                }
            });
        }
</script>

PHP 是这样的:

<?php
header('Content-Type:text/plain');
echo "Hello this is a response!!";
?>

我已经花了几个小时了,但我无法获得 php 中的纯文本作为响应。我也尝试过 JSON.stringify(result) ,我看到的只是一个相当复杂的对象,其中包含请求和嵌入其中的各种文本,而且它没有任何地方包含“您好,这是一个响应!”

这是JSON.stringify(结果):

{
    "request" : {
        "options" : {
            "method" : "post",
            "asynchronous" : true,
            "contentType" : "application/x-www-form-urlencoded",
            "encoding" : "UTF-8",
            "parameters" : "param={\"xxxxxx"]}",
            "evalJSON" : true,
            "evalJS" : true
        },
        "transport" : {
            "statusText" : "",
            "responseText" : "",
            "response" : "",
            "onabort" : null,
            "readyState" : 4,
            "upload" : {
                "onloadstart" : null,
                "onabort" : null,
                "onerror" : null,
                "onload" : null,
                "onprogress" : null
            },
            "onerror" : null,
            "status" : 0,
            "responseXML" : null,
            "onprogress" : null,
            "onload" : null,
            "withCredentials" : false,
            "onloadstart" : null,
            "responseType" : ""
        },
        "url" : "http://xxxxx/xxxxx.php",
        "method" : "post",
        "parameters" : {
            "param" : "{xxxxxx}"
        },
        "body" : "param={xxxxxx}",
        "_complete" : true
    },
    "transport" : {
        "statusText" : "",
        "responseText" : "",
        "response" : "",
        "onabort" : null,
        "readyState" : 4,
        "upload" : {
            "onloadstart" : null,
            "onabort" : null,
            "onerror" : null,
            "onload" : null,
            "onprogress" : null
        },
        "onerror" : null,
        "status" : 0,
        "responseXML" : null,
        "onprogress" : null,
        "onload" : null,
        "withCredentials" : false,
        "onloadstart" : null,
        "responseType" : ""
    },
    "readyState" : 4,
    "status" : 0,
    "statusText" : "",
    "responseText" : "",
    "headerJSON" : null,
    "responseXML" : null,
    "responseJSON" : null
}

最佳答案

如果您尝试此操作,您无法从 PHP 获得纯文本结果?

onSuccess : function(result){
    $('some_id').update(result.responseText);
}

result.responseText 应该只是一个字符串,表示从 PHP 脚本返回到原型(prototype)的 Ajax.Request 的所有内容。

关于php - 原型(prototype) Javascript 框架 - 获取 PHP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7523774/

相关文章:

php - 将 Cloudfront 与动态图像大小调整和 S3 存储集成

javascript - 是否可以从浏览器控制台更改 JS 变量值?

javascript - 使用 Aurelia 进行全局变量订阅

git grep : Sometimes empty result even when the word exists in a file

java - 如何使用java代码修改SOAP响应?

php - 在 URL 中使用连字符

php - 在 PHP 中创建链接

javascript - 如何通过视频标签缓存整个视频文件以便以后离线播放?

javascript - 如何使用 mousedown 事件选中复选框?

json - 在 WSO2 ESB 中聚合后无法获得 JSON 格式的响应