ajax调用永远不会回发到服务器

时间:2015-07-14 21:07:52

标签: javascript jquery asp.net ajax asp.net-mvc-3

我正在尝试将ajax调用回服务器以将新数据保存到数据库中: 这是我的代码:

#include <iostream>;

using namespace std;

void newBalance(int a, int& b, int& c, int& d, int& e, int& f);
void accountInfo(int a, int& b, int& c, int& d, int& e, int& f);

int main()
{
int accountNumberEnter;
int beginBalanceB = 0;
int itemsChargedC = 0;
int creditsD = 0;
int credLimitE = 0;
int balanceD = 0;
int exitLoop = true;

while (exitLoop = true)
{
    cout << "Search for an account number: ";
    cin >> accountNumberEnter;
    cout << endl;

    switch (accountNumberEnter)
    {
    case 1001:
        beginBalanceB = 100;
        credLimitE = 500;
        accountInfo(accountNumberEnter, beginBalanceB, itemsChargedC,     creditsD, credLimitE, balanceD);
        newBalance(accountNumberEnter, beginBalanceB, itemsChargedC,     creditsD, credLimitE, balanceD);
        break;
    case 1002:
        beginBalanceB = 200;
        credLimitE = 1000;
        accountInfo(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        newBalance(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        break;
    case 1003:
        beginBalanceB = 700;
        credLimitE = 750;
        accountInfo(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        newBalance(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        break;
    case 1004:
        beginBalanceB = 1500;
        credLimitE = 1000;
        accountInfo(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        newBalance(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        break;
    case 1005:
        beginBalanceB = 565;
        credLimitE = 2000;
        accountInfo(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        newBalance(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        break;
    case 1006:
        beginBalanceB = 1500;
        credLimitE = 1000;
        accountInfo(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        newBalance(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        break;
    case 1007:
        beginBalanceB = 5000;
        credLimitE = 10000;
        accountInfo(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        newBalance(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        break;
    case 1008:
        beginBalanceB = 0;
        credLimitE = 850;
        accountInfo(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        newBalance(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        break;
    case 1009:
        beginBalanceB = 85;
        credLimitE = 1000;
        accountInfo(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        newBalance(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        break;
    case 1010:
        beginBalanceB = 250;
        credLimitE = 3000;
        accountInfo(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        newBalance(accountNumberEnter, beginBalanceB, itemsChargedC, creditsD, credLimitE, balanceD);
        break;
    default:
        cout << "Account not found.\n" << endl;
        exitLoop != true;

    }
    }


system("pause");

return 0;

}

void accountInfo(int a, int& b, int& c, int& d, int& e, int& f)
{
cout << "Account found\n" << endl;

cout << "Current balance: $"<< b <<"\n" << endl;

cout << "Enter items charged: $";
cin >> c;
cout << endl;

cout << "Enter credits: $";
cin >> d;
cout << endl;
}

void newBalance(int a, int& b, int& c, int& d, int& e, int& f)
{

    int balance;
    int account;

    balance = b + c - d;

    if (balance < 0)
    {
        cout << "Error. New balance is less than 0. Please try again.\n" <<     endl;
    }
    else
    {
        cout << "Account Number: " << a << endl;
        cout << "Beginning Balance: $" << b << endl;
        cout << "Items Charged: $" << c << endl;
        cout << "Credits Applied: $" << d << endl;
        cout << "New Balance: $" << balance << endl;
        cout << "Credit Limit: $" << e <<"\n" << endl;

        if (balance > e)
            cout << "\n***Credit Limit Exceeded***" << endl;
    }

}

我在“SaveNewApplication”函数中设置了一个断点,但断点永远不会被击中! 这是“SaveNewApplication”功能:

$.ajax({
    type: 'POST',
    url: '@Url.Action("SaveNewApplication")',
    contentType: 'application/json; charset=utf-8',
    data: {
        appName: newAppName,
        appDesc: newAppDesc,
        expire: newDaysToExpire,
        displayNote: newDspNote,
        adminRole: newAdminRole,
        defualtRole: newDefaultRole,
        active: newIsActive
    },
    dataType: '',
    success: function () {
        alert("new data has been saved");
    },
    error: function () {
        alert("Error happened whiles saving the new application data");
    }
});

永远不会执行ajax调用中的成功函数

2 个答案:

答案 0 :(得分:1)

如果您将contentType指定为json,请确保您实际上正在发送JSON。 JSON.stringify() converts javascript values into JSON strings

$.ajax({
    type: 'POST',
    url: '@Url.Action("SaveNewApplication")',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({
        appName: newAppName,
        appDesc: newAppDesc,
        expire: newDaysToExpire,
        displayNote: newDspNote,
        adminRole: newAdminRole,
        defualtRole: newDefaultRole,
        active: newIsActive
    }),
    dataType: '',
    success: function () {
        alert("new data has been saved");
    },
    error: function () {
        alert("Error happened whiles saving the new application data");
    }
});

答案 1 :(得分:0)

我发现问题是什么,其中一个传递的参数为null,因此调用永远不会回到服务器。