Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6742909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:53:55+00:00 2026-05-26T11:53:55+00:00

I created two methods on a Javascript class: this.saveData = function(){ var url_send =

  • 0

I created two methods on a Javascript class:

this.saveData = function(){
    var url_send = 'm1=1&m2=2'
    $.ajax({
        url: '/save.php',
        dataType : "text",
        data:url_send,
        type:'POST',
        success: function(data) {
            // this is does not correct
            this.showAcceptBox('error_msg_0');
        }
    });
};

this.showAcceptBox = function(msg_id){
    $('#error_box').removeClass('alert-negative');
    $('#error_box').html($('#'+msg_id).html());
    $('#error_box').show();
    setTimeout(function(){
        $('#error_box').fadeOut('slow',function(){
            $('#error_box').addClass('alert-negative');
        });
    },this.message_box_timeout);
};

How do I correct the call method from my class into jQuery .ajax()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:53:56+00:00Added an answer on May 26, 2026 at 11:53 am

    Try to capture this in a closure:

    this.saveData = function() {
        var url_send = { m1: 1, m2: 2 };
        var _self = this;
        $.ajax({
            url: '/save.php',
            dataType : 'text',
            data: url_send,
            type: 'POST',
            success: function(data) {
                _self.showAcceptBox('error_msg_0');
            }
        });
    };
    

    or pass it as parameter using the context switch:

    this.saveData = function() {
        var url_send = { m1: 1, m2: 2 };
        $.ajax({
            url: '/save.php',
            dataType : 'text',
            data: url_send,
            context: this,
            type: 'POST',
            success: function(data) {
                this.showAcceptBox('error_msg_0');
            }
        });
    };
    

    Quote from the documentation:

    context

    This object will be made the context of all Ajax-related callbacks. By
    default, the context is an object that represents the ajax settings
    used in the call ($.ajaxSettings merged with the settings passed to
    $.ajax). For example specifying a DOM element as the context will make
    that the context for the complete callback of a request, like so:

    $.ajax({
      url: "test.html",
      context: document.body,
      success: function(){
        $(this).addClass("done");
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a javascript object var spanglist = { one: q1, two:q2, three:q3,
I've recently created these two (unrelated) methods to replace lots of boiler-plate code in
alt text http://home.elka.pw.edu.pl/~pkolodzi/Capture.PNG I have created the IAdder interface with two methods: PassA and
I have created two simple files in php. first file (i.e. input.php ): Created
I have a model called Details, and two controller methods new and create. New
I created two tables: Publisher PK, PublisherId, int, not null, indentity +1 PublisherName, varchar(50),
I created two files in the App_GlobalResources folder: SiteResources.en-US.resx SiteResources.sp-SP.resx Both contain a value
I created two very simple Heroku apps to test out the service, but it's
I've created two functions to load expanded views of a month in the archive
I've created two classes, with one of them having an implicit cast between them:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.