简体   繁体   中英

Ajax post with array of parameters, then referenced within PHP

I have written a javascript function that helps me write ajax posts, within this function it accepts an array of parameters. Function is as follows;

/**
* quick function to call an ajax function with post, and get data back via json
* @param {string} url - url of ajax call or php file
* @param {array} data - array of data to pass in a key value pair
* @callback {function} success_callback - on success
* @callback {function} error_callback - on error
* @callback {function} complete_callback - on complete
* @return boolean
*/      
    function ajax_json_post(url, data, success_callback, error_callback, complete_callback) {
        $.ajax ({
            type:"POST",
            url: url,
            data: { data: data},
            dataType: "json",
            success:function(response) {                               
                if(typeof success_callback === "function") {
                    success_callback(response);

                }

                return true;

            },
            error:function(jqXHR, textStatus, errorThrown) {
                if(typeof error_callback === "function") {
                    error_callback(jqXHR, textStatus, errorThrown);

                }

                return false;

            },
            complete:function() {  
                if(typeof complete_callback === "function") {
                    complete_callback();

                }

                 return true;

            }  

        });

    }

The code I'm using to reference this function is below, and it works, however when posting to a PHP script, the array is accessible via $_POST['data']['value1'];

My question is, how can I pass these parameters into the ajax post, but be able to access them without having to declare the array name within PHP, expected would be $_POST['value1']

            var data = {
                value1: 1,
                value2: true

            }


            ajax_json_post("ajax/webservice_quote_domestic.php", data, function(response) { //success
                 if(typeof callback === "function") {
                     callback(response);

                 }

            }, function() { //error


            }, function() { //complete


            });     

Simple

Just change this code and pass data: data, and not data: { data: data},

function ajax_json_post(url, data, success_callback, error_callback, complete_callback) {
    $.ajax ({
        type:"POST",
        url: url,
        data: data,              // change is here
        dataType: "json",
        success:function(response) {                               
            if(typeof success_callback === "function") {
                success_callback(response);
            }
            return true;
        },
        error:function(jqXHR, textStatus, errorThrown) {
            if(typeof error_callback === "function") {
                error_callback(jqXHR, textStatus, errorThrown);
            }
            return false;
        },
        complete:function() {  
            if(typeof complete_callback === "function") {
                complete_callback();

            }
             return true;
        }  
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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