ajax傳輸json到後臺

前端:php

    <script>
    $.fn.serializeObject = function()    
    {    
       var o = {};    
       var a = this.serializeArray();    
       $.each(a, function() {    
           if (o[this.name]) {    
               if (!o[this.name].push) {    
                   o[this.name] = [o[this.name]];    
               }    
               o[this.name].push(this.value || '');    
           } else {    
               o[this.name] = this.value || '';    
           }    
       });    
       return o;    
    };  
       document.getElementById("hand").onclick = function() {
    var request = new XMLHttpRequest();
    request.open("POST", "../controller/post.php");
    var jsonuserinfo = $('#form1').serializeObject();
    var data = JSON.stringify(jsonuserinfo);
    request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    request.send(data);
    request.onreadystatechange = function() {
        if (request.readyState===4) {
            if (request.status===200) { 
                document.getElementById("postResult").innerHTML = request.responseText;
            } else {
                alert("發生錯誤:" + request.status);
            }
        } 
    }
}
</script>

後臺:html

<?php
error_reporting(0);
require_once('../model/PdoMySQL.class.php');
require_once('../model/config.php');
header("Content-Type: text/html;charset=utf-8"); 
if ($_SERVER["REQUEST_METHOD"] == "POST"){
    create();
}
//建立籠位申請信息
function create(){
    //判斷信息是否填寫徹底
    $data=file_get_contents("php://input");
   ...
   //忽略後面代碼
相關文章
相關標籤/搜索