首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从API接收数据并通过AJAX发送到另一个页面

从API接收数据并通过AJAX发送到另一个页面
EN

Stack Overflow用户
提问于 2019-02-21 00:56:40
回答 1查看 655关注 0票数 0

当通过名为asanatarget.php的文件中的POST方法在我的工作区中创建事件时,我正在接收来自API (asana)的数据

数据是正确的,我可以在收到时将其存储在文件中。看起来是这样的:

代码语言:javascript
复制
    {"events":"resource":xxx,"user":xxx,"type":"story","action":"added","created_at":"2019-02-20T14:48:09.142Z","parent":xxx}]}

在同一个文件中,我使用带有GET方法的AJAX将数据发送到一个新文件:

asanatarget.php

代码语言:javascript
复制
<?php 
    if(isset($_SERVER['HTTP_X_HOOK_SECRET'])) {
        $h = $_SERVER['HTTP_X_HOOK_SECRET'];
        header('X-Hook-Secret:' . $h);
        exit;
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    </head>
    <body>
<?php
 $input = file_get_contents('php://input');

    if ($input) {
        $entries = json_decode(file_get_contents('php://input'), true);
        file_put_contents('targetasanaDATA' . time() . '.txt', json_encode($entries));
?>
    <script>
        $( document ).ready(function() {
            $.ajax({
                type: "GET",
                url: "/asanawebhook", // Working with laravel, the route is well defined
                data: <?php echo json_encode($entries); ?>,
                dataType: "json",

                success: function(response){ 
                    console.log("success " + response);
                },

                error: function(jqXHR, textStatus, errorThrown) {   // What to do if we fail
                    console.log(JSON.stringify(jqXHR));
                }

            });
        });  
    </script>


<?php 
} 

?>
</body>
</html>

当我用测试数据直接加载asanatarget.php时,它工作得很好,数据被传递给/asanawebhook,但是当数据直接从api传递过来时,它就不工作了。我检查过了,数据总是正确的

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-21 04:22:44

PHP脚本只生成一个HTML页面(基本上是一个文本)。

javascript可以由浏览器解释和执行。但是,如果没有浏览器读取并执行此页面,则不会发生任何事情。PHP生成一个网页,没有人阅读它,事情就到此为止了。

您也可以使用PHP通过POST发送数据。您可以使用http_build_query()file_get_contents()构建查询。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54791625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档