后台PHP取不到数据,求高手解答

	<select id="news_type" >新闻类型
     <option value="1">推荐</option>
     <option value="2">科技</option>
     <option value="3">文学</option>
     <option value="4">图片</option>
    </select>
    <label for="news_title">新闻标题</label>
    <input type="text" id="news_title" name="news_title" />
    <label for="news_content">新闻内容</label>
    <textarea id="news_content" name="news_content" ></textarea>
    <label for="news_source">新闻来源</label>
    <input type="text" id="news_source" name="news_source" />
    <label for="news_image">新闻图片</label>
    <input type="text" id="news_image" name="news_image" />
    <label for="publish_time">发布时间</label>
    <input type="text" id="publish_time" name="publish_time" />

<input type="button" value="添加" id="add" />
$(function(){
	$("#add").on("click",function(){
		$.ajax({
			type:"POST",
			url:"add_handler.php",
			dataType:"json",
			data:{
				type:$("#news_type").val(),
				title:$("#news_title").val(),
				content:$("#news_content").val(),
				source:$("#news_source").val(),
				image:$("#news_image").val()
			},
			success:function(data){
				if(data){
						console.log(data);
					}else{
						alert("error");
					}
			},
			error:function(jqXHR){
				alert("发生错误:"+jqXHR.status);
			}	
		});
	});
});
<?php
	//require_once("../connect.php");
	//添加
	$type = $_POST['type'];
	$title = $_POST['title'];
	$content = $_POST['content'];
	$source = $_POST['source'];
	$image = $_POST['image'];
	$time = time();
	//echo $type;
?>

请问我的代码写错了,为什么在我本地后台PHP取不到数据。前台已经成功发送数据

http://img.mukewang.com/578df1db0001188c07600486.jpg

后台一直报错

http://img.mukewang.com/578df1f20001de2909400559.jpg

把代码发给别人的环境看过,别人能取到数据。请问是我本地的环境问题吗?装的是wamp集成开发包

tyc1
浏览 2977回答 8
8回答

柠檬酸钠

我觉得这种情况下var_dump一下$_POST最有用了

茫海

你报错的截图是那来的,你用ajax请求,然后ajax的dataType设置为json,然而你后台返回的数据并不是json,所以目测会执行error里面的内容

乔帮主

从你的这个截图上看,数据是有的,也提交到后台了!你首先除掉这个Notice吧: $type = isset($_POST['type'])?$_POST['type']:'取不到数据';另外的几条也这样试试看!然后在测试下试试,看报什么错!

我聴聞妳始终一个人

你这边环境报错级别设置的太高。所以才会出现notice找不到。当你级别设置高的时候,使用变量都必须先定义。比如:$arr=array();$arr[]=array("name"=>"王小二","age"=>23);

Alie0

看你的代码,php并没有调用这些变量为何会提示变量未定义,你在控制台那找到发送的请求右键在新页面打开看看效果将信息打印一下看看

_潇潇暮雨

肯定是本地环境问题啊。
打开App,查看更多内容
随时随地看视频慕课网APP