Dispatch an Ajax request but encounter a model internal error

I am currently working on an mvc3 project and encountering issues with sending an ajax post request from my JS code. The problem lies in the fact that I am receiving an internal error, and upon debugging, I noticed that the method is not being invoked.

Below is my JS code snippet:

ajaxCallTest: function (url, dataObj) {
    $.ajax({
        url: url,
        type: "POST",
        data: dataObj,
        success: function (result) {
        }
        ,
        error: function (result) {
        }
    });
}

And here is my C# code snippet:

[HttpPost]
public string Transaction(PaymentModel model) 
{
...
}

Upon testing using the following lines of code:

ajaxCallTest('..url/Transaction', ''); <- enters 'Transaction' with null values...

However, when I run:

ajaxCallTest('..url/Transaction', model); <--- the model is of type PaymentModel 

The second code does not trigger the 'Transaction' method, instead resulting in a 500 internal error message.

Can anyone shed some light on why this might be happening?

Edited:

var dataObj = { Id: 11, Description: "hh" };

$.ajax({
        url: url,
        type: "POST",
        data: JSON.stringify(dataObj),
        success: function (result) {
            //window.location.replace(result);
            var form = $(result);
            $(form).submit();
        }
        ,
        error: function (result) {
            console.log('Error ' + result);
            //cardStrip.fail();
        }
    });

Answer №1

Ensure that your dataObj aligns with your PaymentModel, for example:

If your PaymentModel is structured like so:

public class PaymentModel
{
    var int Id{get; set;}
    var string Description{get; set;}
}

Your JavaScript code should resemble the following:

// create an object that matches PaymentModel
var dataObj = {Id: $('myId').val(), Description: $('#MyDescription').val()};

$.ajax({
    url: url,
    type: "POST",
    data:  JSON.stringify(dataObj)

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

Directive for integrating Amcharts with Angular JS

I have created a custom directive specifically for displaying a single chart using Amcharts. angular.module("App").directive('myElem', function () { return { restrict: 'E', replace:true, temp ...

Is there a way to run a series of Observables one after the other, where each Observable only starts after the previous one has finished?

Suppose I am dynamically generating an array of observables: let obsArray = dataArray.map((data) => observableFunc(data)) I aim to run each observable in sequence, with the next one starting only after the current one finishes. The order of execution ...

Different ways to insert spaces among variables in an angular component

I have a function below that I want to use to return a slice of characters with a count as shown in the example below. Example - abcd,test,red,blue,green,yellow Expected output - abcd,test,red,blue,g.........................(11) My code works correctly ...

How to Send C# Array as a Parameter to a JQuery Function

I'm currently working on passing a C# array to a JQuery function as a parameter. The C# code I have to call the function is: //Create an Array from filtered DataTable Column var GatepassIDs = defaultView.ToTable().AsEnumerable().Select(r => r ...

Is the return type determined by the parameter type?

I need to create an interface that can handle different types of parameters from a third-party library, which will determine the return type. The return types could also be complex types or basic types like void or null. Here is a simple example demonstra ...

Is there a way to switch the video source when a particular h3 tag is clicked?

https://i.sstatic.net/I2gIZ.jpgI'm currently working on building a video streaming site, but I've hit a roadblock. My challenge right now is figuring out how to make the episode selector change the video player's source. I'm a bit conf ...

Implementing Script Loading specifically on a Component (Route) in React

Is it possible to implement HTML scripts in a specific React component rather than directly in index.html? I need to load a third-party barcode scanner script that will only be used in one component, so I'm looking to avoid loading it for every compon ...

Creating a ROT13 cipher in JavaScript

In my JS function, I need to handle a variable called i. My goal is to encode this variable using ROT13 before proceeding with the function. The challenge lies in decoding the variable and integrating it into the script. I came across a JavaScript implem ...

Replacing Identifiers in Entity Framework

I am experiencing a similar issue to the one described in this question: Entity Framework, Foreign Keys, and EntityKeys. My problem involves Foreign ID key values resetting to 0 before data is sent to the database, resulting in the following error: The ...

The URL provided by window.location is not accurate

I'm facing an issue with the code window.history.pushState("", "title", myCtxURLVersion); which is supposed to change the current URL of the page. However, when I implement this code, the URL displayed is incorrect. For example, even though the brows ...

Issue with onClick event not firing when using option tag in React

onClick event seems to have an issue with the <option> tag. How can we successfully use the onClick event with the select option tags while assigning different parameters to each option? async function setLanguage(language) { ...

ReactJS - When a click event is attached to a list of elements, it triggers the "click" event for each individual element in the list

I have a group of li elements under a ul element (as shown below), and when I click on any li element, I want it to add a class name to just that specific li element. However, currently when I click on one li element, the click event is triggered for all l ...

Unfortunately, WordPress Ajax does not support POST requests and cannot pass objects

I've been attempting to access WordPress AJAX from the admin backend while creating a custom post, but I'm encountering some issues. Despite several attempts, my POST request seems to be failing, with the server responding with an error 400 and ...

What is the best way to manage excessive content when printing an HTML page?

Struggling with My Test Project Images () I am in the process of creating a printable test project, however, I am facing an issue when the page content becomes lengthy. I would like the related question to appear on the next page below my header section. ...

An issue arises with Autocomplete when attempting an ajax request and an error is

I'm struggling to implement jQuery Autocomplete on a text box, but it's not functioning properly. Below is my script for retrieving autocomplete list from the database. However, I encounter an error that displays an alert with an error message. ...

Transferring TypeScript modules for browserifying

Recently, I transformed my canvas library from plain JavaScript to TypeScript. I have structured the code using classes, all of which are part of the cnvs module. However, I am facing difficulties in compiling these classes into a single file. My goal is ...

encountering an issue: file or directory does not exist, unable to open 'rds-combined-ca-bundle.pem'

I recently downloaded AWS secure socket for my Node server and integrated it into my index.js folder. However, I encountered an error that reads: "Error: ENOENT: no such file or directory, open 'rds-combined-ca-bundle.pem'. Could someone help me ...

Executing a remote AJAX call in PhoneGap version 3.1.0-0.15.0

Currently, I am working on developing an application using phonegap 3.1.0-0.15.0, and I have encountered an issue with an ajax call to a remote server. Despite completing all the necessary steps such as enabling Internet access and whitelisting the domain ...

Assigning values to elements within foreach loops

I am trying to achieve the following: Having a foreach loop that populates a list of categories and within that, another foreach loop for populating items. With the use of jQuery, When the addItemButton is clicked, I want the entire foreach loop to be h ...

How to Position Input Fields over an Image Using CSS

I am working on positioning input fields over an image to allow data entry directly onto the image itself. https://i.sstatic.net/jW4iM.png Currently, I have a series of input fields (shown above) and three images with different squares on them. My goal is ...