0

我正在尝试将数据从 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", 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>

这种将数据发送到服务器的模式形式

<div id="dialog-form" class="modal hide fade">
        <div class="modal-header">
            <a class="close" data-dismiss="modal">×</a>
            <h3>Add Question</h3>
        </div>
        <div class="modal-body">
            <form  id=gform  >

                <div class="control-group" class="form-horizontal">
                        <label class="control-label" for="question">Write Your Question</label>
                        <div class="controls">
                            <input type="text" class="span3" id="question"
                                name="question" placeholder="Enter Survey Question">
                                <input type="hidden" value=save id="type" name="type" />
                                <input type="hidden" value=12 id="survey_id" name="survey_id" />

                        </div>
                    </div>

                <hr/>
                <label for="name">Write Options</label> 
                            <div class="controls">
                 <input type="text" name="option1"  id="option1"  placeholder="Option 1" />
                <input type="text" name="option2"   id="option2"  placeholder="Option 2" />
                        </div>

                            <div class="controls">
                <input type="text" name="option3"   id="option3" placeholder="Option 3" />
                <input type="text" name="option4"   id="option4" placeholder="Option 4"  />
                        </div>










            </form>

        </div>
        <div class="modal-footer">
            <a href="#" class="btn"  data-dismiss="modal" >Close</a> <a href="#" id="add-ques" class="btn btn-primary">Save
                changes</a>
        </div>

    </div>

这是 PHP 代码,它获取发布的数据并发送到服务器并返回 JSON //question_data.php

    if($type=="save"){
    $type= htmlspecialchars(trim($_REQUEST["type"]));
$question= htmlspecialchars(trim($_POST["question"]));
$survey_id= htmlspecialchars(trim($_POST["survey_id"]));
$opt1= htmlspecialchars(trim($_POST["option1"]));
$opt2= htmlspecialchars(trim($_POST["option2"]));
$opt3= htmlspecialchars(trim($_POST["option3"]));
$opt4= htmlspecialchars(trim($_POST["option4"]));


$result = json_decode(file_get_contents("http://mobsurvey.jelastic.servint.net/question/new/?question=$question&survey_id=$survey_id&option1=$opt1&option2=$opt2&option3=$opt3&option4=$opt4"));
$arr = array('result' => $result->result, 'lastId' => $result->lastId);

    header('Content-type: text/json');
    header('Content-type: application/json');

    echo json_encode($arr);

}

JSON O/P

    {"result": "true","lastId": 86}

有时上面的代码运行良好但有时返回null。无法跟踪此问题

4

1 回答 1

0
//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);
    }
于 2012-04-04T05:03:16.023 回答