"Trouble With JSON and ASP.NET WebMethod: Server-Side Method Not Executing

I am attempting to send parameters to my code behind using the WebMethod.

Although I am successfully reaching the end of ajax, the method in my aspx.cs code behind is not being called and I am encountering an error.

Operation failed! Details: '[object Object]'

I am utilizing a masterpage and wonder if that might be causing any issues.

How can I resolve this issue?

I would greatly appreciate any assistance. Thank you!

Javascript:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=imgfasi]").bind("click", function () {
            var fasi = {};
            fasi.Txseltlc = $("[id*=txseltlc]").val();
            fasi.Txrescldisa = $("[id*=txrescldisa]").val();
            fasi.Ddlauttlc = $("[id*=ddlauttlc]").val();
            $.ajax({
                type: "POST",
                url: "Default.aspx/Savepnfasi",
                data: '{fasi: ' + JSON.stringify(fasi) + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    if (response == "1") {
                        alert("Success!");
                    } else {
                        alert("Operation failed! Details: " + response);
                    }
                }
            });
            return false;
        });
    });
</script>

Code Behind :

public class pnfasiweb
{
    public string Txseltlc { get; set; }
    public string Txrescldisa { get; set; }
    public string Ddlauttlc { get; set; }
}

[WebMethod]
[ScriptMethod]
public static void Savepnfasi(pnfasiweb fasi)
{
    if (!String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["id"]))
    {         
        string ProductID = Mpskmt3.Base64ForUrlDecode(HttpContext.Current.Request.QueryString["id"].ToString());

        // SQL Update command

        using (OdbcConnection cn =
                new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQLlocalhost"].ConnectionString))
        {
            using (OdbcCommand command =
                    new OdbcCommand(sql, cn))
            {
                try
                {
                    command.Connection.Open();                       
                    // Set Parameters
                    command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    command.Connection.Close();
                }
            }
        }
    }
    else
    {
       // Handle Error
    }
}

#Edit01

https://i.sstatic.net/MiGmi.png

Answer №1

Give this a shot:

let queryString = "?" + window.location.href.split("?")[1];
let formData = {};

...

url: "Default.aspx/Savepnfasi" + queryString,
data: '{formData: ' + JSON.stringify(formData) + '}',

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

Display array values in JavaScript from different arrays

I'm currently working on extracting data from my API to plot it in a graph. Specifically, I am interested in obtaining an array that contains only the 'date' values. Here's what I have managed to extract so far: This is the code snippe ...

Is there a way to utilize nodemon without having to install it through npm?

I'm having trouble with my network not allowing me to use npm install. Is there another way for me to install and use nodemon? I've noticed that Node only runs after setting PATH variables on Windows. I attempted to set the path for nodemon, but ...

Issue with nextElementSibling not applying CSS style

My current issue revolves around a button that is designed to open or close a collapsible div. The HTML structure of this element looks like the following: <div class='outer-collapsible'> <button type='button' class='col ...

Is there a way for users to share their thoughts in response to the posts of

I'm looking to implement a feature that allows users to add comments on posts with an "add comment" button similar to what you see in platforms like Facebook. When the button is clicked, a small input box should open up and display the comment below t ...

Using THREE.js in the pre-render function

I am encountering difficulties with updating the positions of my enemies before rendering them. Instead of creating a separate update() function, I attempted to use an onBeforeRender() function attached to each enemy object. However, nothing seems to be ...

Decoding deeply nested JSON arrays in Swift

Having trouble figuring out how to parse JSON using Swift. The goal is to extract the RouteKey and routeNAME from this XML content. I've experimented with various tutorials and attempted to utilize quicktype, but nothing seems to be working. { "respo ...

Utilize jQuery and JSP (or PHP) to showcase detailed information about a specific item when a user clicks on it within an HTML page

For my upcoming college project, I am tasked with developing a movie library. The main page of the library will feature various movie posters. Upon clicking on a poster, users will be directed to a dedicated page displaying detailed information about the ...

What is the best way to bring a module into an Angular project?

I have a project in Angular with an additional module created as an npm package. The structure of the module is as follows: --otherModule --other-module.module.ts --index.ts --package.json index.ts: export { OtherModule } from './other-module ...

What specific checks and alerts are triggered by React.StrictMode?

When utilizing React.StrictMode and React.Fragment, according to the React documentation: Both Fragment and StrictMode do not display any visible UI. Instead, they trigger additional checks and warnings for their child components. Question: What specif ...

When restarting the React application, CSS styles disappear from the page

While developing my React application, I encountered a problem with the CSS styling of the Select component from Material UI. Specifically, when I attempt to remove padding from the Select component, the padding is successfully removed. However, upon refre ...

Is there a way to retrieve the disabled drop-down field value after submitting the form?

I'm struggling with a dropdown field that becomes disabled once an item is selected. After submitting the form, the dropdown field loses its value and I'm left with an empty field. Any suggestions on how to keep a value in the dropdown after subm ...

Guide to designing a bar graph using HTML5 for a web app optimized for iPad

I am in the process of converting my iOS native app for iPad to a web app using HTML5. However, I am encountering an issue with creating a bar graph for the HTML5 app. Is there any documentation or built-in API available for this specific task? On iOS, we ...

What is the best way to transform a UIImage into a JSON file on an iPhone device

When working with the NSJSONSerialization class, I have run into a limitation where only certain types like NSString, NSNumber, NSArray, NSDictionary, or NSNull are supported for conversion to JSON. Unfortunately, my object includes an additional field of ...

AngularJS modal behaving oddly when checkboxes are used

My Angular app is available in this plunker. Upon clicking the button, a modal dialog opens displaying a list of items. Two of these items are pre-checked based on conditions set in the checkbox table input. The following function handles pushing and spl ...

Using dynamic jquery to target specific elements. How can we apply jquery to selected elements only?

Hello everyone! I have been working on a simple hover color change effect using jQuery, but I noticed that I am repeating the code for different buttons and service icons. Is there a way to achieve the same result so that when a button is hovered, the co ...

What is the best way to include double quotation marks to create a valid JSON string in Ruby?

Whenever I send a JSON string to the third-party app, it ends up removing all the double-quotes from it. As a result, the response I get back is in this format: '{key:value, key2:value2}' This makes it impossible for me to parse the data as JS ...

Substitute placeholders in array with information using a loop

I have a question regarding implementing an autosort feature in JavaScript. I want my page to automatically sort data rows based on different time intervals selected by the user through checkboxes. The data updates every 3 seconds, and the autosort functio ...

Bring back object categories when pressing the previous button on Firefox

When working with a form, I start off with an input element that has the "unfilled" class. As the user fills out the form, I use dynamic code to remove this class. After the form is submitted, there is a redirect to another page. If I click the "back" ...

Unlocking the attributes of Angular form elements: A guide

Imagine we have a form like this <form name="myForm" data-ng-controller="Ctrl"> <input name="input" data-ng-model="userType" data-description="User Type" required> </form> When working in the controller, we can refer to the input el ...

The unusual behavior of the :to attribute on @click in Vue

I have a specific element: Hashtag.vue: <template> <router-link :to="getTo" custom v-slot="{ navigate }"> <div role="link" @click="navigate"> {{text}}</div> </rout ...