jQuery $.ajax(), pass success data into separate function

前端 未结 6 796
难免孤独
难免孤独 2020-12-02 13:23

I am using the jQuery $.ajax() function. I have put this into a parent function, which passes some values into the ajax function. What I would like to do, is have a user d

相关标签:
6条回答
  • 2020-12-02 14:02

    Works fine for me:

    <script src="/jquery.js"></script>
    <script>
    var callback = function(data, textStatus, xhr)
    {
        alert(data + "\t" + textStatus);
    }
    
    var test = function(str, cb) {
        var data = 'Input values';
        $.ajax({
            type: 'post',
            url: 'http://www.mydomain.com/ajaxscript',
            data: data,
            success: cb
        });
    }
    test('Hello, world', callback);
    </script>
    
    0 讨论(0)
  • 2020-12-02 14:06

    I believe your problem is that you are passing testFunct a string, and not a function object, (is that even possible?)

    0 讨论(0)
  • 2020-12-02 14:06

    Although I am not 100% sure what you want (probably my brain is slow today), here is an example of a similar use to what you describe:

    function GetProcedureById(procedureId)
    {
        var includeMaster = true;
        pString = '{"procedureId":"' + procedureId.toString() + '","includeMaster":"' + includeMaster.toString() + '"}';
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: pString,
            datatype: "json",
            dataFilter: function(data)
            {
                var msg;
                if (typeof (JSON) !== 'undefined' &&
                        typeof (JSON.parse) === 'function')
                    msg = JSON.parse(data);
                else
                    msg = eval('(' + data + ')');
                if (msg.hasOwnProperty('d'))
                    return msg.d;
                else
                    return msg;
            },
            url: "webservice/ProcedureCodesService.asmx/GetProcedureById",
            success: function(msg)
            {
                LoadProcedure(msg);
            },
            failure: function(msg)
            {
                // $("#sometextplace").text("Procedure did not load");
            }
        });
    };
    /* build the Procedure option list */
    function LoadProcedure(jdata)
    {
        if (jdata.length < 10)
        {
            $("select#cptIcdProcedureSelect").attr('size', jdata.length);
        }
        else
        {
            $("select#cptIcdProcedureSelect").attr('size', '10');
        };
        var options = '';
        for (var i = 0; i < jdata.length; i++)
        {
            options += '<option value="' + jdata[i].Description + '">' + jdata[i].Description + ' (' + jdata[i].ProcedureCode + ')' + '</option>';
        };
        $("select#cptIcdProcedureSelect").html(options);
    };
    
    0 讨论(0)
  • 2020-12-02 14:15

    You can use this keyword to access custom data, passed to $.ajax() function:

        $.ajax({
            // ... // --> put ajax configuration parameters here
            yourCustomData: {param1: 'any value', time: '1h24'},  // put your custom key/value pair here
            success: successHandler
        });
    
        function successHandler(data, textStatus, jqXHR) {
            alert(this.yourCustomData.param1);  // shows "any value"
            console.log(this.yourCustomData.time);
        }
    
    0 讨论(0)
  • 2020-12-02 14:15

    this is how I do it

    function run_ajax(obj) {
        $.ajax({
            type:"POST",
            url: prefix,
            data: obj.pdata,
            dataType: 'json',
            error: function(data) {
                //do error stuff
            },
            success: function(data) {
    
                if(obj.func){
                    obj.func(data); 
                }
    
            }
        });
    }
    
    alert_func(data){
        //do what you want with data
    }
    
    var obj= {};
    obj.pdata = {sumbit:"somevalue"}; // post variable data
    obj.func = alert_func;
    run_ajax(obj);
    
    0 讨论(0)
  • In the first code block, you're never using the str parameter. Did you mean to say the following?

    testFunc = function(str, callback) {
        $.ajax({
            type: 'POST',
            url: 'http://www.myurl.com',
            data: str,
            success: callback
        });
    }
    
    0 讨论(0)
提交回复
热议问题