1
    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.

5
  • 1
    response==form_data.username Commented Nov 13, 2011 at 17:35
  • 1
    How about form_data.username ? Commented Nov 13, 2011 at 17:35
  • please answer your solution, not comment. thank you Commented Nov 13, 2011 at 17:38
  • the solution is not worthy of an answer really. an answer could be an essay of how to access object properties in javascript or similar Commented Nov 13, 2011 at 17:38
  • its an answer regardless of whether its worth or not. Commented Nov 13, 2011 at 17:40

2 Answers 2

2
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) {
Sign up to request clarification or add additional context in comments.

4 Comments

this appears to be simply a copy/paste of the jQuery from the OP
@KaeVerens: look at the last line
It's worth mentioning to the OP that this comparison will only work if "response" (what the server returns) is only the username as a string. You might want to structure your response to accomodate future information as well, even if the username is all you care about at this moment.
Agree with @GregPettit . When the dataType of the ajax request is text, I usually trim the response. like this: if( $.trim(response)==form_data.username) Because response often comes with blank spaces at the beginning and at the end of it.
1

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) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.