有时post数据返回null、Jquery、PHP
我正在尝试将数据从PHP发布到服务器,该服务器是使用jquery在java中构建的,它使用json进行响应。 遵循PHP,Jquery发布代码有时post数据返回null、Jquery、PHP,php,jquery,json,post,file-get-contents,Php,Jquery,Json,Post,File Get Contents,我正在尝试将数据从PHP发布到服务器,该服务器是使用jquery在java中构建的,它使用json进行响应。 遵循PHP,Jquery发布代码 <script type="text/javascript" charset="utf-8"> $(function() { // wire up the buttons to dismiss the modal when shown $("#dialog-form").bind("show", f
<script type="text/javascript" charset="utf-8">
$(function() {
// wire up the buttons to dismiss the modal when shown
$("#dialog-form").bind("show", function() {
$("#dialog-form #add-ques").click(function(e) {
// do something based on which button was clicked
// we just log the contents of the link element for demo purposes
$.post('question_data.php', $("form#gform").serialize(), function(data) {
if(data.result=="true"){
//window.location=+data.lastId;
console.log(data);
}else{
$("#dialog-form").modal('hide');
}
},"json");
// hide the dialog box
$("#dialog-form").modal('hide');
});
});
</script>
JSON O/p
{"result": "true","lastId": 86}
上面的代码有时运行得很好,但有时返回空值。无法跟踪此问题感谢您的回复@ubercooluk。。仍然返回null值not error(即您的帖子有一些问题),这是用jquery在java中构建的
//Track it on the server side
if($type=="save"){
$surveyname= htmlspecialchars(trim($_POST["name"]));
$company_id= htmlspecialchars(trim($_POST["company_id"]));
$flag = 1;
if($surveyname == null || $surveyname == ""){
$flag = 0;
}
if($company_id == null || $company_id == ""){
$flag = 0;
}
if($flag){
$result = json_decode(file_get_contents("http://localhost:8080/mobsurvey/survey/new/?name=$surveyname&company_id=$company_id"));
$arr = array('result' => $result->result, 'lastId' => $result->lastId);
header('Content-type: text/json');
header('Content-type: application/json');
}else{
$arr = array('Your post has some problems..not posting some of the values');
}
echo json_encode($arr);
}
{"result": "true","lastId": 86}
//Track it on the server side
if($type=="save"){
$surveyname= htmlspecialchars(trim($_POST["name"]));
$company_id= htmlspecialchars(trim($_POST["company_id"]));
$flag = 1;
if($surveyname == null || $surveyname == ""){
$flag = 0;
}
if($company_id == null || $company_id == ""){
$flag = 0;
}
if($flag){
$result = json_decode(file_get_contents("http://localhost:8080/mobsurvey/survey/new/?name=$surveyname&company_id=$company_id"));
$arr = array('result' => $result->result, 'lastId' => $result->lastId);
header('Content-type: text/json');
header('Content-type: application/json');
}else{
$arr = array('Your post has some problems..not posting some of the values');
}
echo json_encode($arr);
}