Php 获取json中的Javascript变量
我有一个Php 获取json中的Javascript变量,php,javascript,jquery,json,wordpress,Php,Javascript,Jquery,Json,Wordpress,我有一个json文件,其中大约有20个对象,如下所示: { "firstName": "User1", "lastName" : "UserLname", "title": "Human", "description": "Something Facinating", "photoURL": "http://www.example.com/wp-content/themes/mytheme/images/profile/user1.jpg", "contact": { "email"
json
文件,其中大约有20个对象,如下所示:
{
"firstName": "User1",
"lastName" : "UserLname",
"title": "Human",
"description": "Something Facinating",
"photoURL": "http://www.example.com/wp-content/themes/mytheme/images/profile/user1.jpg",
"contact": {
"email" : "user1@example.com"
}
}
我有一个javascript代码来将这些对象的图像/描述显示到页面上。我想这个网站被上传到多个地方。所以在这个json
文件中使用绝对url对我来说没有意义。我通过从header.php
文件中传递变量templateUrl
并在javascript文件中调用它,克服了js
中的问题
<script type="text/javascript">
var templateUrl = '<?= get_bloginfo("template_url"); ?>';
</script>
var templateUrl='';
在javascript中:$.getJSON(templateUrl+“/scripts/file.json”,函数(文件){..}
我还需要一种方法将此
templateUrl
变量传递到json
文件。这样,我可以将图像路径设置为justimages/profile/user1.jpg
,并且我可以根据站点上载的位置将url预先发送到该文件。我还需要一种方法将此模板变量传递到json文件
这个JSON文件是从哪里来的?它也在脚本文件夹中。它有一个如上所述的对象数组。只需打开你的文件.JSON
并进行编辑以删除绝对路径……或者这个文件是动态生成的?不,脚本文件不是动态生成的。我担心的不是JSON的位置,而是位置图像的位置。更改“photoURL”:http://www.example.com/wp-content/themes/mytheme/images/profile/user1.jpg“
至“photoURL”:“images/profile/user1.jpg”
在JSON文件中。否则,我想我误解了你的问题。这与我的问题有什么关系?我仍然不能使用JSON
中的变量,对吗?或者我在这里遗漏了什么?对不起,我希望我没有误解你的问题。不过,请看一下编辑后的建议,它现在不那么通用了。本质是将“file.json”更改为“whatever.php”以传递变量。我希望这会有所帮助。这并不能完全回答我的问题。但我知道从这里开始该怎么做。谢谢。)
#json.php
{
"firstName": "User1",
"lastName" : "UserLname",
"title": "Human",
"description": "Something Facinating",
"photoURL": "<?= $_GET['var']; ?>/images/profile/user1.jpg",
"contact": {
"email" : "user1@example.com"
}
}
#main page
<script type="text/javascript">
var templateUrl = encodeURI("http://example.com");
$(function(){
$.getJSON(
'json.php?var=' + templateUrl,
function(data){
$.each(data, function(key, val){
console.log(key + ": " + val);
});
}
);
});
</script>
firstName: User1
lastName: UserLname
title: Human
description: Something Facinating
photoURL: http://example.com/images/profile/user1.jpg
contact: [object Object]