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 9293687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:11:52+00:00 2026-06-18T21:11:52+00:00

When executing an ajax request using jQuery I am not sure how to add

  • 0

When executing an ajax request using jQuery I am not sure how to add a variable to the data that is being sent to the server. Sending a request with a static string works fine:

$("#btnSubmit").click(function () {

                var inputtext = $("#txtInput").val();

                $.ajax({
                    type: "POST",
                    url: "Webform1.aspx/Function",
                    data: "{'ans':'hello'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        alert(msg.d);
                    }

                });
            });

The above calls the .net function and then produces a msgbox with the response string.

How can I replace the hard coded string ‘hello’ with a variable such as the inputtext variable above.

The following does not work and results in 500 errors that relate to json parsing:

{'ans':inputtext}

{ans:inputtext}

{"ans":inputtext}

(ans is the name of the function parameter)

Edit:

To process the request server side I just use the .net WebMethod attribute for the function, which works with the hard-coded string request in the above example but simply returns the entire html page when passed a variable using data: {ans:inputtext}:

    [System.Web.Services.WebMethod]
    public static string Function(string ans)
    {

        return ans;
    }
  • 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-06-18T21:11:54+00:00Added an answer on June 18, 2026 at 9:11 pm

    This should work :

    $("#btnSubmit").click(function () {
        var inputtext = $("#txtInput").val();
    
        $.ajax({
            type: "POST",
            url: "Webform1.aspx/Function",
            data: {ans: inputtext},
            dataType: "json",
            success: function (msg) {
                alert(msg.d);
            }
        });
    });
    

    As long as #btnSubmit is’nt an actual submit button in a form.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am executing an AJAX request using jQuery as such: $.get({ url: 'run_program.php', data:
Using jQuery's $.ajax() function. Wherether the request has been purposely aborted, or if the
I am fetching 2 div's from a page using $.post jQuery AJAX request. Now
I have a page that uses an AJAX request (using Prototype's Ajax.Request()) to validate
I have a website that uses Jquery and Python. I have an Ajax Request
I'm developing a website using PHP+Ajax. I have to fire a ajax request that
I'm removing certain records using a webservice. The jquery ajax request is written in
I am currently executing AJAX query stores on each onblur if this.value is not
In the following piece of JavaScript code, i'm executing GetData.php using AJAX. However, when
I'm trying to read the http-response by ajax request. As an echo-server I use

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.