使用Ajax向JavaScript发送JavaScript数组返回Null

时间:2018-01-17 00:32:31

标签: javascript php jquery json ajax

我尝试将一个JS数组发送到php,但是post数据一直在返回null。这是我的JavaScript代码:

console.log($Seats);
console.log($Seats.toString());
console.log(JSON.stringify({ $Seats }));
var jsonStr = JSON.stringify($Seats);
 $.ajax({
    url     : 'database-files/addseatmaps.php',
    type    : 'POST',
    data    : {seater: jsonStr } ,
    success : function (data) {

    console.log(data);


    },error: function( error )
{
                                        //$("#loadingmessage").modal('hide');
$.Notification.autoHideNotify('error', 'top right', 'Submission Error','Check Internet Connection');
    submit.ladda( 'stop' );
                    },
    cache: false,
    processData: false,  
    contentType: false
    });
     return false;

这是我的PHP代码:

<?php
require_once("../classes/mysql.class.php");
session_start();

$insert = new MYSQL();
$busexist = new MySQL;

//echo date_default_timezone_get();

$d=strtotime("now");
$date =  date("d/n/Y H:i:s", $d);
$time =  date("H:i:s", $d);


$myArray = json_decode($_POST['seater']);

var_dump($myArray);
?>

这是我得到的输出:

<pre class='xdebug-var-dump' dir='ltr'>
<small>D:\wamp64\www\lorrybus_admin\database-files\addseatmaps.php:22:
</small><font color='#3465a4'>null</font>
</pre>

我不知道我做错了什么,但我已经尝试了一切可能仍然没有结果。

1 个答案:

答案 0 :(得分:0)

删除

Information:Gradle tasks [:app:assembleDebug]
Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Lcom/loopj/android/http/AsyncHttpResponseHandler;
Error:com.android.dex.DexException: Multiple dex files define Lcom/loopj/android/http/AsyncHttpResponseHandler;
Error:  at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:661)
Error:  at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:616)
Error:  at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:598)
Error:  at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
Error:  at com.android.dx.merge.DexMerger.merge(DexMerger.java:198)
Error:  at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:61)
Error:  at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:36)
Error:  at java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1424)
Error:  at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
Error:  at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
Error:  at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
Error:  at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Lcom/loopj/android/http/BaseJsonHttpResponseHandler;
Error:com.android.dex.DexException: Multiple dex files define Lcom/loopj/android/http/BaseJsonHttpResponseHandler;
Error:  at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:661)
Error:  at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:616)
Error:  at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:598)
Error:  at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
Error:  at com.android.dx.merge.DexMerger.merge(DexMerger.java:198)
Error:  at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:61)
Error:  at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:36)
Error:  at java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1424)
Error:  at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
Error:  at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
Error:  at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
Error:  at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Error:Execution failed for task ':app:transformDexArchiveWithDexMergerForDebug'.
> com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Lcom/loopj/android/http/BaseJsonHttpResponseHandler;

您需要jquery来处理您的数据以转换为正确的类型(application / x-www-form-urlencoded),并且您需要设置内容类型。