dongtanxi5676756 2014-05-08 16:44
浏览 47
已采纳

TideSDK使用/ Ajax调用PHP文件

So, TideSDK say that php is preprocessed upon each request (if it's a .php file).

I'm using the following JS ajax:

function ajax(url, method, data, async)
{
method = typeof method !== 'undefined' ? method : 'GET';
async = typeof async !== 'undefined' ? async : false;

if (window.XMLHttpRequest)
{
    var xhReq = new XMLHttpRequest();
}
else
{
    var xhReq = new ActiveXObject("Microsoft.XMLHTTP");
}


if (method == 'POST')
{
    xhReq.open(method, url, async);
    xhReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhReq.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    xhReq.send(data);
}
else
{
    if(typeof data !== 'undefined' && data !== null)
    {
        url = url+'?'+data;
    }
    xhReq.open(method, url, async);
    xhReq.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    xhReq.send(null);
}

return xhReq.responseText;
console.log("[ajax] Request Completed.");
}

and my index.php file is:

<?php echo "Test"; ?>

ajax is called as such

console.log(ajax('index.php', 'GET'));

Instead of returning 'Test' it just returns the source code.

Am I doing something wrong, or is this expected. Other-wise, what could I do too get the expected output: the pre processed PHP.

  • 写回答

1条回答 默认 最新

  • douhui4831 2014-05-08 16:52
    关注

    If you want to do a ajax get request on your php script, use the jQuery ajax method.

    RTM: http://api.jquery.com/jquery.ajax/

    Example GET Request:

    $(document).ready(function()
    {
        $.get("index.php", function(data) {
            console.log(data);
        });
    });
    

    Example POST Request:

    $(document).ready(function()
    {
        $.ajax({
            type: "POST",
            url: "index.php",
            data: { "test": "hello world", "anotherkey": "andvalue" },
            success: function(data) {
                console.log(data);
            }
        });
    });
    
    // usage in php: $post_test = $_POST['test']; // returns 'hello world'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用