duanli4146 2014-11-25 09:42
浏览 46

将配置选项从文件传递到javascript

In my PHP project, there is a javascript code that is supposed to use some values defined in a config file. So, I'm looking for best way to to pass config values to javascript, without mixing php and js. I was thinking that this might be accomplished with Node.js, where config file would be read and parsed, and then this Node module would be converted into browser JS with browserify. Is this even possible ?

  • 写回答

1条回答 默认 最新

  • dqqxkq4047 2014-11-25 09:46
    关注

    If you don't want to mix PHP code with JS code you can run ajax call in javascript to a php function that returns JSON with data you need.

    End of function in PHP would be something like:

    exit(json_encode($data));
    

    and in javascript (with jquery):

    $.post("script.php", {}, function(data) {
        var configParams = JSON.parse(data);
    });
    
    评论

报告相同问题?

悬赏问题

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