Accessing ASP.net web services using JavaScript client

I have a client-server application that was developed by someone else, with the client side using JavaScript. I now need to create a new web service in ASP.net, but I'm unsure about how to call web methods. This situation is similar to what's described in this Stack Overflow thread, which unfortunately doesn't provide a clear answer for my specific needs. (It simply suggests using WCF, but I'm unfamiliar with how to implement it) (Note that my client app is completely separate from the server, imagine it being written in Eclipse) I am seeking guidance on how to call the default HelloWorld method of an ASP.net web service from a simple HTML code that includes JavaScript.

Thank you, Ela

Answer №1

Check out the following resources for a comprehensive guide on calling web services using JavaScript/Asp.net Ajax or jQuery.

Here is some sample code:

if (window.XMLHttpRequest) {
    // for IE7+, Firefox, Chrome, Opera, Safari
    this.xmlhttp = new XMLHttpRequest();
}
else {
    // for IE6, IE5
    try {
        this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
        try {
            // older version of Msxml
            this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) {
            this.xmlhttp = null;
        }
    }
}
xmlhttp.onreadystatechange = function() {
    /// <summary>
    /// Display server time when success
    /// </summary>
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        // success Status
        alert(xmlhttp.responseText);
    }
}
this.xmlhttp.open("POST", "AjaxServer.asmx/WebMethodName", true);
this.xmlhttp.send();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adding Currency Symbol to Tooltip Data in Material-UI Sparklinechart

Below is a SparklineChart example using MUI library: import * as React from 'react'; import Stack from '@mui/material/Stack'; import Box from '@mui/material/Box'; import { SparkLineChart } from '@mui/x-charts/SparkLineCha ...

How to implement a file upload using AJAX in a Grails application within a g:

I am currently facing a challenge with uploading files via Ajax in a modal window form. I have tried various Grails plugins for ajax file upload but without success. Is there a way to achieve file upload without page refresh using the following code: &l ...

Tips for validating multiple forms on a single page without altering the JavaScript validation structure

My JSP page currently consists of two forms: <body> <form id="1"></form> <form id="2"></form> </body> Only one form is visible at a time when the page is viewed. A JavaScript validation file is used to check the ...

Generate a JSON object specifically for the modified input fields upon submitting the form

Is there a way to generate a JSON object only with the input fields that have been modified before submitting the form? How can I capture and store each changed value in the form as JSON data? $(document).ready(function() { $('#save').clic ...

Transforming a Processing (cursor) file into an interactive webpage

I have created a custom cursor using Processing and now I want to incorporate it into my website. Is there a way to convert the cursor into a .java file so that I can include it in my HTML file? ...

The current context does not recognize the variable

I'm just starting to learn C# .NET and I have a question about how to display an age selection from 1 to 100. Is there a specific way to achieve this? Within the .aspx file, I inserted the following code snippet which utilizes data binding for the va ...

Learning how to toggle default snippet keywords on and off based on specific scenarios within the Angular Ace Editor

Within the Ace editor, I have incorporated custom snippets alongside the default ones. However, there are certain scenarios where I would like to exclusively display the custom snippets and hide the default ones. Is there a way to disable or conceal the ...

Using index value as parameter in append function within angular js

I attempted to develop a chat box in Angular, and although I was able to create it, something feels wrong. When I click on the members, it displays a different member than expected. Could someone please review my code? Here is the link to the Plunker: &l ...

Loop through an array in JavaScript to dynamically create new elements

I'm working on a task that involves creating list element based on the values in an array. Essentially, for each key in the array, I need to generate an li element. <ul class="alpharow"> </ul> $(".alpharow").each(function() { var ...

What is the best method for accessing OpenWeatherMap details via JSON?

I am facing a challenge in JavaScript as I attempt to create a program that fetches weather information from OpenWeatherMap using JSON. My experience with JSON is limited, but I believe I have a grasp of the underlying concepts. Despite this, when I trigge ...

The usage of componentWillMount is no longer recommended

According to the official documentation, componentWillMount is no longer being used and it is recommended to place any code meant for this lifecycle method into the constructor. I'm a bit unsure on how to do that. I have some code intended for compon ...

I am unable to retrieve a list using Jquery's ajax function

Can someone please help me diagnose the issue with the code below and why it's not functioning properly? This code snippet is from a webmethod in an aspx.cs page. [webmethod] [ScriptMethod(ResponseFormat=ResponseFormat.Json)] public sta ...

How can I properly choose distinct values for an individual attribute from a JavaScript array containing objects?

Let's imagine a scenario with an array of JavaScript objects like this: var data = [ {category : "root", type: "qqqqq", value1: "aaaaa", value2: "zzzzz"}, {category : "root", type: "qqqqq", value1: "aaaaa", value2: "xxxxx"}, {category : " ...

What is the best method to generate a distinct identifier for individual input fields using either JavaScript or jQuery?

I have attempted to copy the table n number of times using a for loop. Unfortunately, the for loop seems to only work on the first iteration. I am aware that this is due to not having unique IDs assigned to each table. As a beginner, I am unsure how to cre ...

Passing variables from a C# (ASP.NET 4.5) button event to a JQuery function is something I need to accomplish

I've been struggling to figure out how to pass variables from a C# method on my webpage to a JQuery DateTimePicker. Even though my C# code is functioning correctly, I haven't been able to find a comprehensive tutorial that specifically addresses ...

The ng-show animation is not functioning as anticipated

I've put together this Plunker, but I'm having issues with the enter and leave animations... The enter animation seems to be delayed, while the leave animation doesn't seem to work at all. Here is the CSS style: div.ng-enter { ...

The state is not being configured accurately

In my ReactJs project, I have a model Component with a picture that is displayed. I want to pass the data of the clicked picture, which can be neither raw data nor a URL. I have implemented a handler that can delete the picture (if pressed with the Ctrl k ...

The AngularJs Wizard Validation Inputfield is unable to locate the scope

Hello all, I am new to AngularJs and encountering an issue with my code. Currently, my HTML structure looks like this: <div class="wizardContainer"> <wizard on-finish="finishedWizard()"> <wz-step title="1"> <h1>Questio ...

I'm having trouble grasping the project layout of a Vue project that was generated using vue-cli

Having recently ventured into Vue.js with no prior experience in frontend frameworks, I embarked on a project using the "vue create" command along with standard plugins. Everything seemed to be running smoothly until I decided to start from scratch by dele ...

Having trouble triggering a change event within a React component?

I'm working on a straightforward react-component that consists of a form. Within this form, the user can search for other users. To ensure the form is valid, there needs to be between 3 and 6 users added. To achieve this, I've included a hidden ...