Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have
XML
<button type="button" class="CleanButton" name="exportLink" onclick="ValidateOrder();">
            Complete
        </button>


my javascripts looks like,

JavaScript
function ValidateOrder() {
var jsonObject = { "EmployerName": $("#EmployerName").val(), "Address1": $("Address1").val(), "Address2": $("Address2").val(), "City": $("City").val(), "State": $("State").val(), "City": $("City").val() };  
            $.ajax({
            url: "/Verification/CompleteOrder",
            type: "POST",
            datatype: "json",
            data: JSON.stringify(jsonObject),
            contenttype: "application/json; charset=utf-8",
            success: function (mydata) {
                alert("success");
            },
            error: function () {
               alert("fail");
            }           
        });  }


and my controller looks like
C#
[HttpPost]
public ActionResult CompleteOrder(string jsonData)
{
    return RedirectToAction("Index", "Orders");
}


But I am getting empty json object , am I missing anything?

Rachana
Posted
Comments
ZurdoDev 11-Jul-12 15:20pm    
What happens when you debug the code? Is CompleteOrder actually returning anything?

1 solution

Change the following line

JavaScript
var jsonObject = { "EmployerName": $("#EmployerName").val(), "Address1": $("Address1").val(), "Address2": $("Address2").val(), "City": $("City").val(), "State": $("State").val(), "City": $("City").val() };  


to this


JavaScript
var jsonObject = { "EmployerName": $("#EmployerName").val(), "Address1": $("#Address1").val(), "Address2": $("#Address2").val(), "City": $("#City").val(), "State": $("#State").val() }; 


The issue is that for address 1,2,city and state you do not have the pound '#' sign, which indicates that jQuery should look for an element w/ the "id" specified.

Refer to this fiddle - http://jsfiddle.net/azvzK/[^], it works fine!

2nd issue is change the following

JavaScript
data: JSON.stringify(jsonObject),


to

JavaScript
data: { 'jsonData': JSON.stringify(jsonObject) },


Issue in this case is that the data passed to a method using $.ajax should have the key name matching with the parameter name in your action method

Hope this helps!
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900