November Happy Hour will be moved to Thursday December 5th.

Calling an Block Controller from Ajax

Vote:
 

Hello Folks

I'm trying to call a controller with ajax and it keeps on failing. Anybody can help? Here is the code

Controller Code

public class JumbotronConversionBlockController : BlockController<JumbotronConversionBlock>
    {

....
        [HttpPost]
        public ActionResult Convert(string a)
        {
            return Json("Hello its working!");
        }
....

Html Code

...


            function clickSubmit() {
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("Convert", "JumbotronConversionBlockController")',
                    contentType: "application/json; charset=utf-8",
                    data: { a: "testing" },
                    dataType: "json",
                    success: function() { alert('Success'); },
                    error: function () { alert('Failed'); }
                });
            }


...

ERROR

  1. Request URL:
    https://local.me.com/rendering/JumbotronConversionBlockController/Convert
  2. Request Method:
    POST
  3. Status Code:
    404
  4. Remote Address:
    127.0.0.1:443

#201944
Mar 07, 2019 21:01
Vote:
 

I found the answer. Maybe it could be handy to someone. Actually the ajax call needs to be on the Block not on the Controller.

Here is the updated code:

            function clickSubmit() {
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("Convert", "JumbotronConversionBlock")',
                    contentType: "application/json; charset=utf-8",
                    data: { a: "testing" },
                    dataType: "json",
                    success: function() { alert('Success'); },
                    error: function () { alert('Failed'); }
                });
            }
#201946
Mar 07, 2019 22:33
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.