0

I can't get data from ajax to server side. I don't know much about ajax. This is how I am using it. 'main' is the form tag

<script>
    $('#main').on('submit', function (e) { 
        $.ajax({
            url: "checkout",
            type: "POST", 
            dataType: "json",
            data: JSON.stringify(id),
            success: function (mydata) {
                //history.pushState('', 'checkout' + href, href); 
            }
        });
    );
</script>

[HttpPost]
public JsonResult getData(string id){
    //string id will always be null
    return Json(id, JsonRequestBehavior.AllowGet);
}
2
  • ajax url is checkout while post actionresult is getData.. Commented Aug 4, 2016 at 10:26
  • data: { id: 'someValue' } and url: "@Url.Action("getData")", to generate you url correctly Commented Aug 4, 2016 at 11:23

2 Answers 2

1

please modified function call on submit event..

$("#buttonid").click(function(){  

        $.ajax({
        url: "controllername/getData",
        type: "POST", 
        dataType: "json",
        data: "{id:value}",
        success: function (mydata) {
            //history.pushState('', 'checkout' + href, href); 
        }
    }); });

Give the Valid Url in ajax funation

Sign up to request clarification or add additional context in comments.

Comments

0

Sample Example to you....

You should pass the data with parameters

   var pData = {
            id:"34"
        };

Jquery Code :

  $('#main').on('submit', function (e) {
        var pData = {
            id:"34"
        };
        $.ajax({
            url: "@Url.Content("~/Home/GetData")",
            type: "POST",
            dataType: "json",
            data: pData,
            success: function (response) {
                var item=response;
              //success response
            }
        });
    });

Server Side

public JsonResult GetData(int id)
    {
        var item = new
        {
            elementid = id,
            currenttime = DateTime.Now.ToString()
        };
        return new JsonResult
        {
            Data = item,
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }

I hope this may helpful for you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.