Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

How to post JSON string as a string in jQuery AJAX


Share with WhatsApp


Hello friend, In this short post I will share how to post JSON model as a string to the action method of the controller in MVC using jQuery Ajax.

Suppose you wanted to save user preference as JSON string in one of the column of your table in database, then you can write a action method like following.

[HttpPost]
public void SaveSetup(string jsonString)
{
        //code to save setting in databse
}

And you need to write code like following on some button click to post your JSON model to above action method.

var jsonModel = "your json model";

$.ajax({
            type: 'POST',
            data: JSON.stringify({ jsonString: JSON.stringify(jsonModel) }),
            contentType: "application/json; charset=utf-8",
            url: "/User/SaveSetup/",
            dataType: 'json', 
            success: function (data) {
                alert('saved');
            }
 });

That's it, with above code you can successfully post JSON model as string.

Thanks. Do share your thoughts in comment section below.



If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

What is Hoisting in JavaScript?

Hoisting in JavaScript, one of the most misunderstood or little understood concept in the JavaScript and remain famous as interview question for JavaScript developers and now also for angular js developer. Lets check out what it means exactly.


Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.


Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.


How to call click or any event only once in jQuery

Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.


Assembla - Free and private repository to manage your source code online with SVN subversion hosting

With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.


Your opinion is valuable for us! Comments, suggetions are welcome.


Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
Vu 32'' TV Vu 24'' TV
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!

x