user478636
user478636

Reputation: 3424

accessing variables in jquery ajax

    var form_data = {
        username: $("#username").val(),
        password: $("#password").val(),
        is_ajax: 1
    };

    $.ajax({
        type: "POST",
        url: action,
        data: form_data,
        success: function(response) {
            if (response==username) {

I want to access username variable that i have declared in form_data variable. How can i? i am using $("#username").val() but i want to access the variable.

Upvotes: 1

Views: 224

Answers (2)

Martin.
Martin.

Reputation: 10537

form_data.username is the variable you want

if (response == form_data.username) {

so in result, it will all look like

var form_data = {
    username: $("#username").val(),
    password: $("#password").val(),
    is_ajax: 1
};

$.ajax({
    type: "POST",
    url: action,
    data: form_data,
    success: function(response) {
        if (response == form_data.username) {

Upvotes: 1

rafael.js
rafael.js

Reputation: 600

var form_data = {
        username: $("#username").val(),
        password: $("#password").val(),
        is_ajax: 1
    };

    $.ajax({
        type: "POST",
        url: action,
        data: form_data,
        success: function(response) {
            if (response==form_data.username) {

Upvotes: 2

Related Questions