如何使用jquery将数据从PHP传输到Javascript? [重复]
问题描述:
This question already has an answer here:
I want to transfer data from PHP page which import data from MySql, so I need to show this data on my mobile app (Cordova), I use javascript.
this is the PHP code which I use:
if($count == 1) {
echo $name .$points . $gold;
}else {
echo "Your Login Name or Password is invalid";
}
and this is the Jquery code:
$.ajax({
type: 'POST',
url: 'http://xxxxxx/login.php',
data: datastring,
success: function(data) {
alert(data + 'worked');// does nothing
}
});
</div>
此问题已经存在 这里有一个答案: p>
-
如何将变量和数据从PHP传递到JavaScript?
\ r
19 answers
span>
li>
ul>
div>
我想从中传输数据 从MySql导入数据的PHP页面,所以我需要在我的移动应用程序(Cordova)上显示这些数据,我使用javascript。 p>
这是我使用的PHP代码: p >
p>
if($ count == 1){ echo $ name。$ points。 $ gold; }其他{ echo“您的登录名或密码无效”; } code> pre> div> div>
这是Jquery代码: p>
p>
$ .ajax({ type:'POST', url:'http://xxxxxx/login.php', data:datastring, \ n success:function(data){ alert(data +'working'); //什么都不做 } }}; code> pre> div> div>
- 写回答
- 好问题 提建议
- 关注问题
- 收藏
- 分享
- 邀请回答
答
I found the answer:
1- add 'json_encode' after 'echo' 2- use this code on Javascript:
<script>
function reqListener () {
console.log(this.responseText);
}
var oReq = new XMLHttpRequest();
oReq.onload = function() {
alert(this.responseText);
};
oReq.open("get", "http://localhost/send.php", true);
oReq.send();
</script>
</div>