【问题标题】:passing multiple parameters using $.ajax by POST to php function使用 $.ajax 通过 POST 向 php 函数传递多个参数
【发布时间】:2015-11-23 08:04:27
【问题描述】:

我必须通过 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) {
    }

PHP 中的控制器端

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

PHP 中的函数,模型。

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

我不知道我是否清楚。

【问题讨论】:

  • 你为什么把GETPOST混在一起?

标签: php jquery ajax post


【解决方案1】:

您的 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) {
    }
)}

另外,在您的 PHP 中,您不需要使用 GET,因为您的 ajax 正在发送 POST 请求。从而使GET['action'] 无关紧要。

【讨论】:

    【解决方案2】:
    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);
    });
    

    尝试类似 m8 的代码应该可以帮助你

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-23
      • 2017-11-15
      • 1970-01-01
      相关资源
      最近更新 更多