2013-06-05 32 views
0

我需要将值从PHP传回给我的javascript。我已经尝试了很多东西,但无法使其工作。这是我的代码。需要将值从PHP传递到Javascript

<script type="text/javascript">  
    life = {}; 
    life.anjaxEnter = function(finished) { 
     $.ajax({ 
     dataType:"json", 
     type:"POST", 
     data:mydata, 
     url:"myurl.php", 
     async:false, 
     success:life.receiveResult, 
     }); 
     setTimeout("location.href='myredirectionurl'", 1000); // Redirect after 1 second 
    } 
    life.receiveResult = function(result) { 
     alert(result); 
    } 
</script> 

我需要将PHP文件中的特定值传回给life.receiveResult,然后将其添加到重定向url。

+0

可能重复[如何将这些字符串从PHP传递到JavaScript](http://stackoverflow.com/questions/3174092/how-to-pass-these-strings-from-php-to-javascript)和http ://stackoverflow.com/questions/8626183/passing-values-from-php-to-javascript?rq = 1 –

+2

你有一个额外的逗号后susccess – stackErr

+0

你可以搜索之前发布的问题。我看到这个问题在Stack Overflow上回答了几十次。 –

回答

2

最大的问题可能是,当时当你将life.receiveResultsuccesslife.receiveResultundefined。在将其分配给其他内容之前,您需要对其进行定义。

0

myurl.php调用时需要返回JSON结果。事情是这样的:

<?php 
header("Content-Type: text/json"); 
$mydata = "Hello"; 
echo json_encode(array('ok' => true, 'mydata' => $mydata)); 
?> 

看一看的json_encode文档中,header docs和一些otheranswers以获取更多信息。