Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lewis Smith 211 posts 620 karma points c-trib
    Sep 19, 2017 @ 15:14
    Lewis Smith
    0

    JQuery calling Controller Method

    Hi All,

    I'm trying to call a Controller Method using JQuery . But every time I'm getting something along the lines of:

    WeBuyAlmostAnythingController /FirstAjax 404 (Not Found)

    Below is my code. For the moment all I'm trying to do is return anything at all just so I understand the JQuery side of things.

    Controller:

    public class WeBuyAlmostAnythingController : SurfaceController
    {
       [HttpPost]
       public ActionResult FirstAjax(string a)
       {
          return Json("chamara", JsonRequestBehavior.AllowGet);
       }
    }
    

    JQuery:

    $('.item#watches').click(function () {
      var serviceURL = '/WeBuyAlmostAnythingController/FirstAjax';
      $.ajax({
        type: "POST",
        url: serviceURL,
        data: param = "",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: successFunc,
        error: errorFunc
      });    
      function successFunc(data, status) {
        alert(data);
      }    
      function errorFunc() {
        alert('error');
      }
    });
    

    Currently all I'm getting is error as the errorFunc is being called.

    Thanks, Lewis

  • Lewis Smith 211 posts 620 karma points c-trib
    Sep 19, 2017 @ 15:17
    Lewis Smith
    0

    Am i pointing to the controller correctly?

    Do i need to point to Controllers/WeBuyAlmostAnything.cs? Or do i need to add some routing?

    Thanks, Lewis

  • Lewis Smith 211 posts 620 karma points c-trib
    Sep 19, 2017 @ 15:33
    Lewis Smith
    0

    After reading up on routing I understand now that the route should be the following:

    /umbraco/surface/WeBuyAlmostAnythingController/FirstAjax

    I have tried this and I'm still getting the same error.

    Thanks, Lewis

  • Ayo Adesina 445 posts 1059 karma points
    Sep 19, 2017 @ 15:47
    Ayo Adesina
    0

    What is the error exatly?

    This is just a guess, but your surface control method takes in a string pram, but your not passing that in the Ajax request.

    Could it be that its not finding the method becuase you not passing the prams to match it.

    Try creating another method without any prams and see if that gets called.

    If you post the error it might be easier for someone to give you a pointer.

  • Craig Mayers 164 posts 508 karma points
    Sep 19, 2017 @ 15:49
    Craig Mayers
    0

    Hi Lewis,

    It is probably the parameter field not matching..

    Change "param" to "a" and pass a value.

    Also, this could be changed to a GET request for simplicity and testing.

    Craig

  • Lewis Smith 211 posts 620 karma points c-trib
    Sep 20, 2017 @ 08:13
    Lewis Smith
    0

    I managed to get this working. I deleted everything and started again. The code below is the working code.

    One question: Is there any downfalls to doing this with JQuery? Obviously older browsers or users who have JavaScript disabled wont seek the benefits.

    Controller:

    public class TestSurfaceController : SurfaceController
        {
            [HttpPost]
            public JsonResult MyajaxCall(int order)
            {
                if(order == 1)
                {
                    return Json("1");
                }
                else if (order == 2)
                {
                    return Json("2");
                }
                else
                {
                    return Json("Not found");
                }
            }
        }
    

    JQuery:

    $('.item#watches').click(function () {
            $.ajax({
                url: '/umbraco/Surface/TestSurface/MyajaxCall',
                type: "POST",
                dataType: "json",
                data: {order: 2 },
                success: function (data) {
                    alert(data);
                }, error: function (data) {
                    alert('err');
                }
            });
        });
    

    Any questions,

    Ask away!

    Thanks, Lewis

Please Sign in or register to post replies

Write your reply to:

Draft