Home > Backend Development > PHP Tutorial > 如何用JS获取PHP页面内容?

如何用JS获取PHP页面内容?

WBOY
Release: 2016-06-23 13:22:42
Original
1486 people have browsed it

用http://localhost:8081/1.php可以访问到 ‘admin’ 这个内容,我想用JS把这个内容显示出来,有人说用AJAX来写,百度了好几天也没有解决,请教各位老师如何做到啊?谢谢!


回复讨论(解决方案)

就是使用ajax请求这个地址,请求回来的数据放在页面中就可以了

$.ajax({
             type: "get",
             url: 'http://localhost:8081/1.php',
             dataType: "jsonp",
             jsonp: "callback",
             success: function(data){
             province = data.province;//
       alert(province ); 
             },
               });

用这个语句出不来结果

$php_test='I come from PHP!';  
echo "var province="."'$php_test';";   
?> 


这是PHP

就是使用ajax请求这个地址,请求回来的数据放在页面中就可以了



出不来结果啊

你的 1.php 输出的是 js 脚本,要这样写

$.getScript('1.php', function() {  alert(province)});
Copy after login

$.ajax({
             type: "get",
             url: 'http://localhost:8081/1.php',
             dataType: "jsonp",
             jsonp: "callback",
             success: function(data){
             province = data.province;//
       alert(province ); 
             },
               });

用这个语句出不来结果



province什么鬼。。。直接在success的方法里console.log(data);用chrome浏览器看console栏里打印了什么内容
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template