简体   繁体   English

使用ajax和javascript对象将值传递给另一个php。

[英]pass value to another php using ajax and javascript object..?

when i tried to json_decode the json.stringify data it returned NULL so used **json_last_error to know the error and it returned the following error message 当我尝试对json.stringify 数据进行json_decode时它返回NULL,因此使用** json_last_error来了解该错误,并返回了以下错误消息

Syntax error, malformed JSON 语法错误,JSON格式错误

can anyone verify my ajax code enclosed in javascript function and spot the error ive done. 任何人都可以验证我包含在javascript函数中的ajax代码并发现已完成的错误。

Below is the script code 下面是脚本代码

 <script>
 function callphp(){
 var dataa = {};
 dataa.dateipone = jQuery("#dateInputone").val();
 dataa.dateiptwo = jQuery("#dateInputtwo").val(); 
 dataa.ino = jQuery("#ino").val();
 dataa.submit = "submit";
 alert("Hello")
 $.ajax({
    url : "six-cuf.php",
    type: 'POST',
    data :JSON.stringify(dataa),
    contentType : "application/json; charset=utf-8",
    success:function(data)
    {
        if(data){ 
          alert(data);   
          //console.log("Data from Server"+JSON.stringify(data));
        }
        else{
          console.log("Data is empty");
        } 
    },
    error: function(xhr) {
        alert('Error!  Status = ' + xhr.status + " Message = " + xhr.statusText);
        //console.log('Error!  Status = ' + xhr.status + " Message = " +  xhr.statusText);
    }
   });
 }

Below is six-cuf.php 下面是6-cuf.php

  $data = json_decode($_POST['dataa']);
  print_r($data);

  switch (json_last_error()) {
   case JSON_ERROR_NONE:
         echo ' - No errors';
         break;
   case JSON_ERROR_DEPTH:
         echo ' - Maximum stack depth exceeded';
          break;
   case JSON_ERROR_STATE_MISMATCH:
         echo ' - Underflow or the modes mismatch';
          break;
   case JSON_ERROR_CTRL_CHAR:
         echo ' - Unexpected control character found';
          break;
   case JSON_ERROR_SYNTAX:
         echo ' - Syntax error, malformed JSON';
          break;
   case JSON_ERROR_UTF8:
         echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
          break;
   default:
         echo ' - Unknown error';
         break;
   }

   if(isset($_POST["dateipone"],$_POST["dateiptwo"],$_POST["ino"],$_POST["options"],$_POST["dateone"],$_POST["datetwo"],$_POST["submit"]))
{

    //php code
}

in dateipone,dateiptwo and dateone,datetwo...value of one this pair will be empty. 在dateipone,dateiptwo和dateone,datetwo ...中,该对的值将为空。

There's no need to encode anything into json before passing to server. 在传递给服务器之前,无需将任何内容编码为json。 You can pass plain javascript object, browser will do the rest: 您可以传递普通的javascript对象,其余的将由浏览器完成:

var dataa = {};
dataa.dateipone = jQuery("#dateInputone").val();
dataa.dateiptwo = jQuery("#dateInputtwo").val(); 
dataa.ino = jQuery("#ino").val();
dataa.submit = "submit";
$.ajax({
    url : "six-cuf.php",
    type: 'POST',
    data: dataa,
    // no content-type here!
    success:function(data) {

    }
});

On server side check $_POST array as usual: 在服务器端照常检查$_POST数组:

print_r($_POST);
// you will have keys: `dateipone`, `dateiptwo`, `ino` and `submit` in it

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM