jQuery的提交POST不形jQuery、POST

2023-09-10 20:41:14 作者:難以、釋懷

我想提交一份张贴到PHP脚本,而无需使用一种形式。该功能可以正常工作除了实际交的部分。

I'm trying to submit a POST to a PHP script, without using a form. The function works correctly apart from the actual post part.

有人能看到什么一定是错了什么,我在这里做什么?

Can anyone see what must be wrong with what I am doing here?

function checkforRates(){
alert('activated');
//FUNCTION CHECKS FOR RATES FOR EACH OF THE ICPS IN LATEST CONTRACT
var count = $("#selectICP").children("option:not([disabled])").length;
success = 0
$('#selectICP option:not([disabled])').each(function() {
    opICPs = $(this).val();
    $.ajaxSetup({
        type: 'POST',
        URL: 'functions/workforce_newcontract.php',
        data: {'checkrates': 'true', 'ICP': opICPs, 'ctype': ctype},
        //data: '?checkrates=true&ICP=' + opICPs + '&ctype=' + ctype,
        success: function(result) {
            if(result == 1){    
                //THIS ICP HAS ALL METERS AND ENGINES WITH RATES
                success = success + 1;
            } else {       
                $('#contract_window_message_error_mes').html(result);
                $('#contract_window_message_error').fadeIn(300).delay(3000).fadeOut(700);
                return false;
            }
        },
        error: function() {
            $('#contract_window_message_error_mes').html("An error occured, the form was not submitted.");
            $('#contract_window_message_error').fadeIn(300).delay(3000).fadeOut(700);
        }
    });
    if(success === count){
        //CONTINUE ONTO NEXT STAGE
        alert('Success!');
    }
});
}

非常感谢。

推荐答案

首先,你要调用错误的函数。你需要调用 $。阿贾克斯(),不是 $。ajaxSetup()

First, you're calling the wrong function. You need to call $.ajax(), not $.ajaxSetup().

二,你不提供正确的参数。具体做法是:

Second, you are not providing the correct arguments. Specifically:

    URL: 'functions/workforce_newcontract.php',

正确的属性名称是网​​址,不是网​​址

    url: 'functions/workforce_newcontract.php',

三,肯祥指出,你不能正确处理异步的一部分。该位的code:

Third, as Ken Cheung pointed out, you're not handling the asynchronous part correctly. This bit of code:

if(success === count){
    //CONTINUE ONTO NEXT STAGE
    alert('Success!');
}

需要是的在的你的成功()函数,而不是在 $。阿贾克斯()电话。

needs to be inside your success() function, not after the $.ajax() call.