在AJAX调用中未收到发送的数据

时间:2016-11-01 12:20:47

标签: php jquery ajax prestashop-1.6

我正在尝试在Prestashop 1.6中创建和调用AJAX,并调用将保存所有ajax相关代码的ajax.php文件。我的jQuery函数包含$ .ajax({})是

    function setCustomCarrierMethod(method_id){
          if (method_id) {
            $.ajax({
              url: baseDir + '/modules/customcarrier/ajax.php',
                    type: 'POST',
                    data: 'ajax_function=set_customcarrier_method' + 'method_id=' + method_id,
                    dataType: 'json',
              success: function(json) {
                console.log("successfull request");
              },
              error: function(json) {
                alert(json.error);
                console.log("error in the request");
              }
            })
          }
        };

然后在ajax.php文件中我得到了:

<?php
session_start();
/* SSL Management */
$useSSL = true;

require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../config/smarty.config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');
include_once(dirname(__FILE__).'/customcarrier.php');

$customcarrier = new CustomCarrier();

if (Tools::getValue('ajax_function') == 'set_customcarrier_method') {
    $customcarrier->setCustomCarrierMethod();
 }

?>

任何想法我在这里遇到的错误都将受到高度赞赏!

1 个答案:

答案 0 :(得分:0)

你必须使用&amp;在两个变量之间

 data: 'ajax_function=set_customcarrier_method' & 'method_id=' + method_id

function setCustomCarrierMethod(method_id){
          if (method_id) {
            $.ajax({
              url: baseDir + '/modules/customcarrier/ajax.php',
                    type: 'POST',
                    data: 'ajax_function=set_customcarrier_method' & 'method_id=' + method_id,
                    dataType: 'json',
              success: function(json) {
                console.log("successfull request");
              },
              error: function(json) {
                alert(json.error);
                console.log("error in the request");
              }
            });
          }
        }