Error message in JQuery Ajax: "Invalid request to web service, parameter value for 'Object' is missing"

I'm struggling to successfully post a form to my web service. Before sending it to the server, I am converting the form into an object. However, I encounter an error when trying to post the form as an Object to my Asmx web service.

Here is my Ajax code:

 var formObject = $(form).serializeObject();
 var formData = JSON.stringify(formObject);
 $.ajax({
          type: "POST",
          url: "./Service.asmx/PostAutomaticRule",
          data: { myObject: formData },
          contentType: "application/json; charset=utf-8",
          dataType: "json",
                success: function (response) {
                    alert("success");
                },
                error: function (response) {
                    alert("Error");
                }
            });

And here is my asmx endpoint:

  [WebMethod(EnableSession = true)]
    public void PostAutomaticRule(MyClass myObject)
    {
        Debug.WriteLine(myObject);
    }


}

[Serializable]
public class MyClass
{
    public string automaticRuleName { get; set; }
    public string action { get; set; }
    public string increaseBudgetByValue { get; set; }
    public string increaseBudgetMaximumBudget { get; set; }
}

Upon debugging, these are the values that pop up:https://i.sstatic.net/wQpS2.png.

Any ideas on what could be going wrong?

Answer â„–1

Below is a sample code snippet that you can use for the information parameter in your ajax request:

info: {myData: JSON.stringify($(form).serializeObject())},

Answer â„–2

After reviewing tips on sending JSON collections to an ASMX webservice

I suggest making this adjustment:

data: {Object : JSON.stringify($(form).serializeObject())}

or

data: {myObject : JSON.stringify($(form).serializeObject())}

It may be necessary to create a more specific class as well.

Answer â„–3

After some trial and error, I was able to achieve the desired result with this code:

$.ajax({
          type: "POST",
          url: "./Service.asmx/PostAutomaticRule",
          data: "{'myObject': "+formData+"}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
                success: function (response) {
                    alert("success");
                },
                error: function (response) {
                    alert("Error");
                }
            });

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

Highlight all the written content within the text box

I'm struggling with a piece of code that is supposed to select all the text inside an input field: <input id="userName" class="form-control" type="text" name="enteredUserName" data-ng-show="vm.userNameDisplayed()" data-ng-model="vm.enteredUs ...

Steps to refresh your nodejs project after making changes

As a newcomer to nodejs, I find myself constantly restarting the server every time I make changes to my HTML files or when using twig, jade, or ejs template engines. Does anyone have any suggestions on how to see these changes in the browser without having ...

Using Selenium to interact with a link's href attribute through JavaScript

New to Java and Selenium, I'm facing difficulties when trying to click on a link with JavaScript in href attribute. Here's the snippet from the page source: href="javascript:navigateToDiffTab('https://site_url/medications','Are y ...

Extracting certain elements from a text: a beginner's guide

I am currently developing a task manager that includes a feature to generate a PDF file using jsPDF. I am facing the challenge of extracting specific attributes from a string in order to print them as text utilizing jsPDF. The provided string is: [{" ...

What is the best way to transform a synchronous function call into an observable?

Is there a conventional method or developer in RxJS 6 library that can transform a function call into an observable, as shown below? const liftFun = fun => { try { return of(fun()) } catch (err) { return throwError(err) } ...

Exploring Selenium with C#: How to continuously loop until an element is no longer on the screen

Working with Selenium and C# to run a series of tests, I encountered an issue that has me stuck. In one scenario, a report is generated and a spinner control is displayed on the page while the data is being processed. Once the process is complete, the spin ...

Using Laravel 8 to create connected dropdown menus with the power of Ajax

Struggling with setting up a dependent dropdown menu in Laravel 8 using Ajax. The first dropdown works fine, but the next two don't display any options. Being new to Laravel, I'm having trouble pinpointing the problem areas. Seeking assistance to ...

Establishing global Kendo UI Window settings for all instances

I have been working extensively with Kendo UI windows and I am curious if there is a way to set default values on a global level. Alternatively, could I establish a parent window with predefined values and then selectively override the ones I want to chang ...

Discover the process of dynamically importing JavaScript libraries, modules, and non-component elements within a Next.js

Lately, I have been utilizing Next.js and mastering its dynamic import feature for importing components with named exports. However, I recently encountered a particular npm package that functions only on the client-side (requires window) and has a substant ...

Canvas Frustratingly Covers Headline

Several months ago, I successfully created my portfolio. However, upon revisiting the code after six months, I encountered issues with its functionality. Previously, text would display above a canvas using scrollmagic.js, and while the inspector shows that ...

What is the best way to eliminate the background color from one span after selecting a different one?

I created a span element that changes its background color when clicked, but I want the background color to switch to the newly clicked span while removing it from the previously clicked one. Can someone help me achieve this? CSS list-sty ...

How can I efficiently display three items per click when tapping into jQuery data?

Here is the code that I have been working on: jQuery(document).ready(function( $ ) { var $container = $('#homegrid').masonry({ isAnimated: true, itemSelector: '.home-blocks', columnWidth: '.grid-sizer ...

Slide both divs simultaneously from left to right

Is there a way to simultaneously hide and show div elements, without having to wait for the effect to take place? Here is my current code: $('a').on('click', function(){ var div_hide = $(this).parent(); var div_show = $(this). ...

Missing CSS in dynamically loaded content on IE

I have been searching everywhere, but I just can't seem to find a satisfactory answer to this particular question. Whenever I load a few items on the page, they are displayed correctly at first. However, if a user decides to change the language, the ...

When making a JQuery - Ajax get request, I did not receive the "extracts" or "exintro" (summary) property in the response

Lately, I've been working on a small web application that displays search results from Wikipedia on the webpage after entering a search term into a text field. This has been a project that I’ve dedicated a lot of time to. I have configured an ajax g ...

User-initiated closure of popup triggers error during Google sign in

After successfully implementing Google signin locally, I encountered an issue when attempting to login on a server. The error message displayed was: 'Uncaught: popup closed by user' Despite disabling adblockers and other potential interference, ...

What causes unescaped HTML characters to become unescaped when using $('div :not(script)').contents().filter(function()?

Developing a Chrome Extension that utilizes a click-to-call API, I have encountered an issue where certain pages are displaying unusual behavior. After investigating, I have identified this specific code snippet as the source of the problem. var rxpCtc = n ...

Retrieving Mouse Coordinates using Ajax in PHP

I'm wondering if it's feasible to send an Ajax request with mouse coordinates using PHP. For instance, I am fetching a page with cUrl and would like to trigger a mouse movement event on that page. At this point, I haven't written any code ...

Establish a connection to eDirectory using the credentials of the application, followed by verifying the identity

I am in need of connecting to NetIQ eDirectory using .NET & C# and the connection should be opened with application credentials. Once the connection is established, I must validate user credentials under the authority of those application credentials utili ...

Guide to authenticating Cashfree gateway's webhook signature using JavaScript

I have integrated the Cashfree payments gateway successfully. However, I am unsure how to verify the signature of webhooks. https://i.stack.imgur.com/TxdTx.png This is their recommended approach. Could someone guide me on writing JavaScript code for this ...