2

I've trying to make a POST request. It is returning the same page, i.e the whole HTML instead of the result I send via the Controller.

@model kgSitecoreTraining.Models.BlogVotes



<div class="blog-vote">

        <div id="vote-form">
            <input type="button" class="up" id="up" name="up" value=@Model.Up>
            <input type="button" class="down" id="down" name="down" value=@Model.Down>
        </div>

</div>
<script>

    window.addEventListener('load',
        function () {
             $('#up').on('click', function (event) {
            $.ajax({
                type:"POST",
                url: window.location.href,
                context: this,
                data: "{ scController: 'BlogVotes', scAction: 'test', option: 'op'}",
                success: function (data) {
                    console.log(data);
                },
                error: function (error) {
                    alert(error);
                    console.dir(error);
                }
                 });
            event.preventDefault();
        });
        }, false);
</script>

And this is the controller

using kgSitecoreTraining.Models;
using Newtonsoft.Json;
using Sitecore.Links;
using Sitecore.Mvc.Presentation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace kgSitecoreTraining.Controllers
{
    public class BlogVotesController : Controller
    {
        // GET: BlogVotes
        public ActionResult Index()
        {
            return View(CreateModel());
        }

        [HttpPost]
        public JsonResult test(string option)
        {
            return Json(option);

        }

        private BlogVotes CreateModel() {
            var item = RenderingContext.Current.Rendering.Item;
            var model = new BlogVotes()
            {
                Up = Int32.Parse(item["Up"]),
                Down = Int32.Parse(item["Down"]),
                UrlPost = LinkManager.GetItemUrl(item).ToString()
            };
            return model;
        }
    }
}

As I said, instead of returning the JSON, It's returning the HTML. Any Idea why?

3

From my point of view it's not a correct way for call your api. Try to sent your request in another way: instead of window.location.href try to call http://{hostname}/api/sitecore/BlogVotes/test via HTTP POST

| improve this answer | |
  • Agree completely. Example could look like this: $.post('/api/sitecore/DropdownCart/RemoveLine', { lineId: lineId }) – Mark Cassidy Oct 19 '17 at 13:03
  • Thanks so much, it worked. Still dont know why the old way return the same page. – Joel Alvarez Myrrie Oct 19 '17 at 15:14
  • 1
    Great! When you are trying to send request to window.location.href, Sitecore resolves your request as a request for render Sitecore item and runs pipelines for render item. Also you can register new route and call your api based on route settings. – Andrei Paliakou Oct 19 '17 at 15:20
  • Thank you so much for taking the time to explain. I understand now. Awesome! – Joel Alvarez Myrrie Oct 19 '17 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.