php 为什么我ajax返回值为1 为什么前台接收alert(data)显示的确实整个网页页面的html代码

WBOY
Release: 2016-06-20 12:31:18
Original
798 people have browsed it

php  为什么我ajax返回值为1 为什么前台接收alert(data)显示的确实整个网页页面的html代码(我使用了smarty   也返回的值  页面也是在smarty模板里面)


回复讨论(解决方案)

在php输出1后面加上exit;就可以了

还是不可以!求指导

那是你对 ajax 请求也做了全程处理
通常只要做业务逻辑即可

  可以说的更具体点吗?   

要看到 admin.php 的代码

class adminController{

public function __construct(){
session_start();
if(!(isset($_SESSION['username']))&&(PC::$method!='login')&&(!$_COOKIE['username'])){
$this->showmessage('请登录后在操作!', 'admin.php?controller=admin&method=login');
}else{
$this->auth = isset($_SESSION['username'])?$_SESSION['auth']:array();
}
}

public function login(){
if (!isset($_POST['submit'])){
VIEW::display('admin/login.html');
}else {
$this->checklogin();
}


public function checklogin(){
$username = daddslashes($_POST['username']);
$password = md5(daddslashes($_POST['password']));
$auth=M('admin')->findOne_by_username($username);
if((!empty($auth))&&$auth['password']==$password){
$_SESSION['username']=$username;
//setcookie('username',$username,time()+3600*12);

echo 1;
exit;
//echo "<script>window.location.href='admin.php?controller=admin&method=adminlist'</script>";
}elseif (empty($auth)){
echo "用户不存在";
exit;
//$this->showmessage("用户不存在",'admin.php?controller=admin&method=login' );
}elseif ((!empty($auth))&&$auth['password']!=$password){
echo "密码错误";
//$this->showmessage("密码错误",'admin.php?controller=admin&method=login');;//密码错误
}

}




admin.php是入口文件
header("Content-type: text/html; charset=utf-8");
//url形式  index.php?controller=控制器名&method=方法名
require_once('config.php');
require_once('framework/pc.php');
PC::run($config);

你在使用某个框架,那么这个框架应该有一个 ajax 模式
通常框架会完成完整的流程,从而输出页面 html
但是 ajax 一般是不需要输出页面的,所以应开启 ajax 模式,以使工作流程发生改变

不  这是我自己写的一套一个简易的微型框架   

在设定的时候  我没有写任何关于ajax的


然后我把mvc的视图层中引入了smarty模板引擎

自己写的,就得补上
ajax 和 普通页面 的输出方式不同,统一处理自然就是有毛病的

如果用ajax请求,那么加上一个参数告诉入口程序admin.php,不要加载模板。

这里,
$.ajax({
url:'admin.php?controller=admin&method=login',
...
});
没有提交  $_POST['submit'])
admin控制器里面的

public function login(){if (!isset($_POST['submit'])){VIEW::display('admin/login.html');}else {$this->checklogin();}	}
Copy after login

方法
则执行 VIEW::display('admin/login.html');
所以,模板加载了。

建议用firefox的firebug或者chrome核心浏览器的开发者工具的newwork,提交一下,看下有没有提交过去submit这个参数。

或者检查一下form1里面有没有name是submit的值

<form  id="form1"  >    <input name="submit" value="ok" /></form>
Copy after login

谢谢  我试试看啊  说不定还得请教你




这是我用firebug弄的

响应啊  submit也都有

写日志一步步跟踪看看。

要保证echo 1之后再没有任何形式的文本输出,然后把对应页面的php结束符号   ?>  去掉

直接把ajax访问地址及参数放浏览器执行看看就知道了,在一步一步调试

ajaxReturn

  可以说的更具体点吗?   

   我也遇到了一样的问题,该怎么解决啊
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!