Ways to extract information using Ajax

I am working with a variable named sid that represents the number of seats. My goal is to pass sid to the test method in the TryJSON.aspx file, manipulate the data within the method, and then return the result to the ajax call. However, I encountered an error when attempting to pass the sid variable.

var sid = jQuery(this).attr('id');
    console.log(sid);
    $.ajax({
        url: "TryJSON.aspx/test",
        type: "POST",
        data: JSON.stringify({ 'noSeat': sid }),
        contentType: "application/json; charset=utf-8",
        success: function (response) {
            var arr = JSON.parse(response.d);
            console.log(arr);
        },
        error: function () {
            alert("sorry, there was a problem!");
            console.log("error");
        },
        complete: function () {
            console.log("completed");
        }

    });

This is the C# code used to receive the noSeat parameter:

[WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string test(string noSeat)
    {
       // return noSeat;
        //JavaScriptSerializer serializer = new JavaScriptSerializer();
       // return new JavaScriptSerializer().Serialize(new { noSeat = noSeat });
    }

I have tried returning just the noSeat as well as using JavaScript serializer, but I keep getting the error message:

An attempt was made to call the method 'test' using a POST request, which is not allowed.

Even when I attempted to return "Success !!" it did not appear in the console and the error persisted. What could be causing this issue?

Answer №1

let id = jQuery(this).attr('id');
console.log(id);
$.ajax({
    url: "GetJSONData.aspx",
    type: "POST",
    data: JSON.stringify({ 'seatNumber': id }),
    contentType: "application/json; charset=utf-8",
    dataType:'json',
    success: function (response) {
        let data = JSON.parse(response.d);
        console.log(data);
    },
    error: function () {
        alert("Oops, something went wrong!");
        console.log("error");
    },
    complete: function () {
        console.log("process completed");
    }

});

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

As for the Pixel Art Creator feature, you can now switch between different modes and

I'm seeking assistance regarding my Pixel Art Maker project. I recently implemented two new features, toggle() and remove(). The issue I'm facing is that when I input numbers to create the grid and click the submit button, the grid is successfull ...

Develop an irregular shape containing an image of a clamp in C#

There is a request to insert an image at a specific position within a defined polygon on another image. The code that has been utilized for this task: private static void MergeImages(string ImageForeground, string ImageBackground) { try { ...

What is the best way to transform a GET request with a query string into a promise?

After successfully making a request with querystring params, I encountered an issue while trying to promisify my request: // Works var Promise = require("bluebird"); var request = Promise.promisifyAll(require("request")); request.get({ url: 'htt ...

Calculate the total value of each individual subject from the checkbox's data-id, presented in a string format and separated by commas

<div> <input type="checkbox" id="Q_1_ck1" value="R" data-id="Environmental Science, Physical Education, Agriculture, Yoga, "> <label class="custom-control-label" for="Q_1_ck1"> ...

Utilizing Zebra Printers for Seamless Integration and Printing in Ext Js

I'm currently working on developing an app in ExtJs that involves printing a PDF label for warehouse purposes using Zebra smartphones connected to printers via Bluetooth. I've been trying to implement Zebra's APIs specifically the BrowserPri ...

Can state values be utilized as content for Meta tags?

I am looking for a way to display image previews and titles when sharing a page link. In order to achieve this, I am using the Nextjs Head Component. The necessary details are fetched on page load and used as content for the meta attributes. let campaign = ...

What is the goal of JSON.parse(JSON.stringify(x))?

During my recent project work, I stumbled upon the following code snippet: let newParams = JSON.parse(JSON.stringify(initialParams)); I'm curious - what exactly does this code achieve? ...

Trouble encountered while retrieving the data structure in order to obtain the outcome [VueJs]

I am facing an issue while trying to save information from an HTTP request into a structure and then store it in local storage. When I attempt to retrieve the stored item and print a specific value from the structure, it does not work! Here is my code: / ...

What is the best way to utilize AJAX for uploading .mp4, .mp3, and .doc files together with additional FormData?

Hello everyone, I have been successfully uploading files (documents and images) using AJAX. However, I am facing some challenges when it comes to uploading videos through AJAX. I have searched extensively on this site for solutions but have not found exact ...

Exploring the creation of dynamic DOM elements within Angular

I'm having trouble figuring out how to create a DOM element in Angular and pass it to jsPlumb, which I'm using to draw charts. When creating connections between nodes using jsPlumb, I also need to create an overlay on these connections. The offi ...

Error encountered while attempting to install material-ui v3.0.3 due to an unexpected termination of the JSON input

I'm currently in the process of installing the most recent stable version of material-ui(v3.03) by running: npm install @material-ui/core. However, I encountered an issue with npm ERR! Unexpected end of JSON input while parsing near '...-/brcast- ...

Adjust checkbox selection using $_GET parameters

I've been working on a university project to manage student registrations using PHP and Ajax. I've created a page that displays all the student data with an option to edit the records (either update or delete). One important parameter for each st ...

Issue: Package 'cairo' not located on EC2 Bitnami MEAN server

Setting up my MEAN application on a Bitnami server has been quite a challenge. During the installation of dependencies, I encountered an error that I just can't seem to resolve. Despite following the instructions provided in the error message, I am st ...

Javascript tabs with clickable links

I have created a page with a series of thumbnails that reveal content below when clicked. The content is positioned absolutely and set to display:none, with javascript changing this for each thumbnail. (I am not very familiar with javascript and learned t ...

What is the best way to prevent excessive rerenders when verifying if database information has been successfully retrieved?

Hey there, I'm encountering an issue where the if statement check in my code is causing a "too many rerenders" problem. I'm trying to create a delay between pulling data from the database and calculating the BMI. Any suggestions on how to resolve ...

Executing SQL queries in JavaScript using PHP functions

Is it allowed, or is it a good practice? It worked for me, but what issues might I face in the future? Just to clarify, I am new to PHP scripting. // button <button type="button" class="btn btn-primary" id="Submit-button" >Save changes</button> ...

Exploring connections between various objects using JavaScript

Currently, I am working with two sets of arrays: $scope.selectedEmployees = ["1001", "1002"]; $scope.selectedTasks = ["Task1", "Task2"]; My goal is to create an array of objects that combine employees and tasks in a many-to-many relationship. The length ...

React.js "universal" component that is able to be generated multiple instances of

I'm struggling to fully understand this problem. Here's the issue: imagine there is an app where I need to generate notifications, dialogs, or other similar elements from my code. One option is to have a "global" component that controls everyth ...

Is it possible for me to determine when all images have finished loading in order to update the isLoaded variable to true?

I am using the template below: <template> <div v-if='isLoaded'> <div @click='selectSight(index)' v-for='(sight, index) in sights'> <img :src="'https://maps.googleapis.com/maps ...

Tips for extracting a variable value from a website with C# code

I'm facing an issue with writing code in Selenium C# (or another approach) to capture a dynamic value from a webpage, such as a clock, and then display that value in my app. The variable on the webpage changes frequently, and I want to show this chang ...