Copied to clipboard

Flag this post as spam?

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


  • Jan Molbech 40 posts 126 karma points
    May 07, 2014 @ 16:45
    Jan Molbech
    0

    Can't call RestExtension method

    I'm trying to call this method

    [RestExtension("AjaxExtensions")]
        public class AjaxExtensions
        {
            [RestExtensionMethod(ReturnXml = false)]
            public static void HelloWorld()
            {
                string foo = "bar";
            }
        }

    with this Ajax Post call

    $.ajax({
                    url: '/base/AjaxExtensions/HelloWorld',
                    type: 'POST',
                    success: function (data) {
                        
                        
                    }
                });

    Just to see if i hit the method but nothing happens.

    The AjaxExtension file is placed in App_Code->Folder->Another folder.

    I'm running Umbraco version 6.1.6

  • Jamie Pollock 174 posts 853 karma points c-trib
    May 07, 2014 @ 16:56
    Jamie Pollock
    100

    Hi Jan,

    Have you tried using a UmbracoApiController (based on .net WebApi) instead? It pretty much replaces the the RestExtensions functionality.

    You can find information on UmbracoWebApiController here: http://our.umbraco.org/documentation/Reference/WebApi/

    It's pretty nifty. Please mark this as the answer if it solves your problem. :)

    Jamie

    EDIT: Here's a rough example

    public class YourApiController : UmbracoApiController {
        public string HelloWorld() {
            return "It's pretty nifty :)";
        }
    }
    

    And your JS:

    $.ajax({
            url: '/Umbraco/Api/YourApi/HelloWorld',
            type: 'POST'
        }).done: function (result) {
            alert(result);
        });
    

    This is using jQuery's Done/Fail/Always as Success is deprecated. You can find out about this here.

  • Jan Molbech 40 posts 126 karma points
    May 09, 2014 @ 08:33
    Jan Molbech
    0

    Your suggestion worked like a charm, thx.

    But.....

    Now i'm trying make a POST with parameters and it doesn't work

    Here's my code

     $.ajax({

                        url: '/Umbraco/Api/Helper/AddRequisitionNumberToOrder/',
                        data: { reqNumber: comment, orderGuid: og},
                        type: 'POST',
                        success: function (data) {
                            alert(data);
    
                        }
                    });
    public class HelperController: UmbracoApiController
        {
            public string AddRequisitionNumberToOrder(string reqNumber, string orderGuid)
            {
                var orderG = new Guid(orderGuid);
                var order = PurchaseOrder.FirstOrDefault(x => x.OrderGuid == orderG);
                if (order != null)
                {
                    order["Rekvisitionsnummer"] = reqNumber;
                    order.Save();
                    return "Rekvisitionsnummer er gemt";
                }
                
    
                return "Det indtastede kunne ikke gemmes. Prøv venligst igen";
            }

    The API method isn't hit.

    It has to be a syntax problem, but i can't find it.

  • Jamie Pollock 174 posts 853 karma points c-trib
    May 09, 2014 @ 10:43
    Jamie Pollock
    0

    It could be the trailing slash on the end of your URL you're pointing to. Upon changing that I was able to hit the API method.

    After that, I got issues posting values to the controller. Which might be related to this:

    http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

    You might also find this useful too:

    http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

    I could be googling the wrong thing here. So feel free to correct me if I am. I might be going off track :/

    When I've used WebAPI recently, I used a GET API call, but that was suitable for my solution. It may not be the case here.

    I hope some of this helps. I'd think about taking the trailing slash off the URL first and see if that gets you any closer to a working solution.

    Thanks,
    Jamie

Please Sign in or register to post replies

Write your reply to:

Draft