使用$傳遞多個參數。ajax通過POST - to php函數

[英]passing multiple parameters using $.ajax by POST to php function


I have to pass data through POST, to a function in PHP, the problem is they don't retrieve the data.

我必須通過POST傳遞數據,到PHP的函數中,問題是它們不檢索數據。

var jsonText = JSON.stringify(origen);
var jsonTextDestino = JSON.stringify(destino);
$.ajax({
    type: "POST",
    url:"/lie/controlador/manejo_de_archivos/controlador.php?action=copiar_archivo",
    data: "origen=" + jsonText + "&destino=" + jsonTextDestino ,
    async: false,
    dataType: "json",
    success: function (jsondata) {
    }

controller Side in PHP

控制器在PHP

if ($_GET["action"] == "copiar_archivo"){
    echo json_encode($controlador-> copiar_archivo($_POST["origen"],    $_POST["destino"]));
}

function in PHP, model.

在PHP函數,模型。

function copiar_archivo($path_o, $path_dest){
    //some code
}

I don't know if i am clear.

我不知道我是否清楚。

2 个解决方案

#1


2  

your ajax code should be rebuilt this way: (Notice the object being sent as your data parameters)

應該以這種方式重新構建ajax代碼:(請注意作為數據參數發送的對象)

var my_object = {"origen": origen, "destino":destino};
$.ajax({
    type: "POST",
    url:"/lie/controlador/manejo_de_archivos/controlador.php?action=copiar_archivo",
    data: my_object ,
    async: false,
    dataType: "json",
    success: function (jsondata) {
    }
)}

Also, in your PHP, you don't need to use GET since your ajax is sending a POST request. Thus making GET['action'] irrelevant.

另外,在PHP中,不需要使用GET,因為ajax正在發送POST請求。因此得到了“行動”無關。

#2


1  

var form = new FormData();
form.append("key1", "val1");
form.append("key2", "val2");

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://test.com/php.php",
  "method": "POST",
  "headers": {},
  "processData": false,
  "contentType": false,
  "mimeType": "multipart/form-data",
  "data": form
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

try something like this code m8 with should help you out

嘗試一些像m8這樣的代碼應該可以幫助你


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/08/28/7209d5e574bb1188c5b5c7c997ec03c0.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com