Yopp88
Yopp88

Reputation: 13

Ajax and MVC 4 ( post request)

I want to send a simple string ( which is xml ) to a controller. I don't know why the breakpoint in Visual Studio is not hit.

Here is the jQuery code :

$.ajax({
            type: "POST",
            url: "BasicWizard/show",
            data: "xml="+xmlResult,
            success: function (data) {
                console.log("Oh yeah !");
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(textStatus, errorThrown);
            }


            });

And here is my method in the controller:

    [HttpPost]
    public ActionResult show(string xml)

   {
       try
       {
           ViewBag.xml = xml;
           return PartialView("showXML");
       }
       catch (Exception)
       {
           return Content("error");
       }


    }

I have just a 500 error in the console.

Upvotes: 1

Views: 498

Answers (2)

Joe Enos
Joe Enos

Reputation: 40383

The post data isn't properly formatted. Try passing in encodeURIComponent(xmlResult) instead of just xmlResult. This will escape out the = and other characters that won't work properly in a post value.

Upvotes: 0

Harshit Tailor
Harshit Tailor

Reputation: 3281

try this :-

data: "{'xml':'" + xmlResult+ "'}",

Upvotes: 2

Related Questions