dthp96899 2015-10-01 02:18
浏览 29
已采纳

如何将javascript回调响应传递给php

how to pass the callback javascript response to php if let's say i'm in a single php file ?. here's a sample snippet of e.g test.php

<html>
<head>
<script>
var _somestuff = _somestuff || [];
            (function () {
                _somestuff.push(['id', blahblah], ['setApiKey',   "blahblah"] 
                );
            var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
            g.type = 'text/javascript';
            g.defer = true;
            g.async = true;
            g.src = 'blahblah.js';
            s.parentNode.insertBefore(g, s);
        })();


             function myCallback(response)
            {

                if (response !== undefined) {
                    var data = JSON.parse(response);
                    var item_ids = data.items;
                    console.log(item_ids);
                }
            }  
   _somestuff.push(['blahblah',"001124","blahblah","myCallback"]);
</script>
</head>
<body>
</body>
</html>  

what I want to happen is, display the data or the item_ids inside the html body using php.. how to do that?, i'm in a single page, I don't want to use ajax or whatever,is that possible ?

  • 写回答

2条回答 默认 最新

  • douxi8759 2015-10-02 02:54
    关注

    I solved the my issue by assigning the myCallback function to a variable like

    var a = function myCallback(response)
                {
    
                    if (response !== undefined) {
                        var data = JSON.parse(response);
                        var item_ids = data.items;
                        console.log(item_ids);
                    }
                };
    

    then passed the variable to

    _somestuff.push(['blahblah',"001124","blahblah",a]);
    

    then inside the myCallback if condition, I just manipulated the html there and loaded the data response :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题