简体   繁体   中英

issue with sending ajax message

Newbie to JQuery / JSON / AJAX so please be nice.

I've pieced together this artwork from examples on SO and other sites, but I'm struggling.

I've created some functions to deal with the AJAX response...

function newOrderSuccess(response) { ... }
function newOrderTimeout() { ... }
function newOrderFail() { ... }

...

Here is the AJAX call:

function sendCallAjaxUsingJson(theUrl, theData, successCallbackFunction, timeoutCallbackFunction, otherErrorCallback, timeoutValueMilli)
{
var successFn = successCallbackFunction; 
var timeoutFn = timeoutCallbackFunction; 
var otherFn = otherErrorCallback;
if(!(typeof successFn === 'function') || !(typeof timeoutFn === 'function') || !(typeof otherFn === 'function')) 
        return false;
$.ajax({
        type: "POST",
        url: theUrl,
        timeout:timeoutValueMilli,
        dataType: 'json',
        data: { json: JSON.stringify(theData) },
        success:successFn(result),
        error: function(x, t, m) {
                   if(t==="timeout") {
                        timeoutFn();
                    } else {
                        otherFn();
                    }
                }
    });

}

My code calls the function as follows:

sendCallAjaxUsingJson("/ordertaker.php", 'submitOrder','newOrderSuccess', 'newOrderTimeout', 'newOrderFail',1000);

The result is..... nothing. I was getting to the newOrderFail() function before I uploaded the ordertaker.php file, but now I get nothing.

Where did I go wrong?

You are passing strings to sendCallAjaxUsingJson instead of functions,

sendCallAjaxUsingJson("/ordertaker.php", 'submitOrder',newOrderSuccess, newOrderTimeout, newOrderFail,1000);

Also you are calling your success function in your ajax call rather that setting it.

success:successFn,

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