如何将php变量添加到javascript中?
这是我的密码:如何将php变量添加到javascript中?,javascript,php,Javascript,Php,这是我的密码: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> <?php $url = "http://api.wunderground.com/api/7d5339867b063fd0/geolookup/conditions/q/UK/".$area.".json"; ?> <script> jQuery(doc
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<?php
$url = "http://api.wunderground.com/api/7d5339867b063fd0/geolookup/conditions/q/UK/".$area.".json";
?>
<script>
jQuery(document).ready(function($) {
$.ajax({
url : ,
dataType : "jsonp",
success : function(parsed_json) {
var location = parsed_json['location']['city'];
var temp_f = parsed_json['current_observation']['temp_f'];
alert("This is an example of a web service outputting using Json. The current temperature in " + location + " is: " + temp_f);
}
});
});
</script>
如何将$url变量添加到位于url:here的javascript代码中
谢谢你只需要这样说:
url : "<?php print $url ?>",
url : "<?=$url?>",
或最佳方式:
url : "<?php echo $url; ?>",
使用json_编码:
或者在您的情况下:
$.ajax({
url: <?=json_encode($url)?>,
dataType: "jsonp"
// ...
});
这是最安全的方法,适用于任何数据数组等。您可以回传出来吗?您是否尝试过url:?可能重复的$url中可能重复的数据需要在双引号之间才能有效:我已输入双引号,请参见pls@Magicprog.fr
var jsVar = <?=json_encode($phpVar)?>;
$.ajax({
url: <?=json_encode($url)?>,
dataType: "jsonp"
// ...
});