Jquery Ajax call doesn't hit the action method/returns entire HTML page

S. ten Brinke

The solution to my problem can be found below.

I can not get my code to work. I need to send an AJAX call to an action method in my controller that has to return a string (for now). But when I activate the AJAX call, it returns the HTML source code of the page I'm currently on And it does not even hit the desired controller action.
This is my code:

Ajax call

<script>

    function addtoCart()
    {
        amount = prompt("Hoeveel producten wilt u toevoegen aan uw winkelwagen?", 1);
        //find product
        var product = {
            "id": '@Model.ID',
            "naam": "@Model.Naam",
            "afbeelding": "@Model.Afbeelding",
            "beschrijving": "@Model.Beschrijving",
            "prijs": "@Model.Prijs",
            "aantal": amount
        };
        $.ajax({
            url: '@Url.Action("storeProductInSession", "Product")',
            type: 'POST',
            data: JSON.stringify(product),
            dataType: 'json', //Right now, this gives an "Unexpected Token < error". This is probably because when the method returns the entire HTML document, it won't be able to render <!doctype html> as JSON.
            contentType: 'application/json; charset=utf-8',
            success: function (result)
            {
                console.log(result);
            },
            error: function (jqXHR, textStatus, errorThrown)
            {
                console.log(product);
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
            },
            async: true
        });

        //sessionStorage.setItem('@Model.ID', JSON.stringify(product)); This is for later
    }

StoreProductInSession(The method that has to be called)

 [HttpPost]
        public string storeProductInSession(object product)/*, string naam, string afbeelding, string beschrijving, decimal prijs, int aantal)*/
        {
            return "This method is going to do session stuff, but for now it just has to return this string";

            //Get the POST input from the AJAX call

            //Add the input to a JSON array

            //add the array to the session

            //Return the array

            //let javascript convert that array to the cart <li>
        }

It still returns the entire HTML page, and even if the object product parameter is incorrect, I would be happy to see that the ajax call actually hits that method. I am thinking that there could be a problem between the call and my routing, but I am not sure. Here's that code:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
name: "Product",
url: "Product/{id}",
defaults: new { controller = "Product", action = "Index" }
);
        routes.MapRoute(
            name: "Categorie",
            url: "Categorie/{id}",
            defaults: new { controller = "Categorie", action = "Index" }
            );
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Welkom", action = "Index", id = UrlParameter.Optional }
        );


    }

If I would even be able to simply give the ID to the StoreProductInSession action, I could work from there. I'm going to work with the user's session, so I think a GET would not be safe enough.

Solution

A big thanks to @Dolan for helping out! The problem was my routing!

Ajax Call

<script>

    function addtoCart()
    {
        amount = prompt("Hoeveel producten wilt u toevoegen aan uw winkelwagen?", 1);
        //find product
        var product = {
            "id": '@Model.ID',
            "naam": "@Model.Naam",
            "afbeelding": "@Model.Afbeelding",
            "beschrijving": "@Model.Beschrijving",
            "prijs": "@Model.Prijs",
            "aantal": amount
        };
        $.ajax({
            url: '@Url.Action("storeProductInSession", "Product")',
            type: 'POST',
            data: JSON.stringify(product),
            dataType: 'html', //I tried entering 'json' but that gave me a 'unexpected < token' error.
            contentType: 'application/json; charset=utf-8',
            success: function (result)
            {
                console.log(result);
            },
            error: function (jqXHR, textStatus, errorThrown)
            {
                console.log(product);
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
            },
            async: true
        });
</script>

storeProductInSession

 [HttpPost]
        public string storeProductInSession(int id, string naam, string afbeelding, string beschrijving, decimal prijs, int aantal)
        {
            return String.Format("Product given to the ajax call: {0}, {1}, {2}, {3}, {4}, {5}", id, naam, afbeelding, beschrijving, prijs, aantal);
}

Routing

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
    name: "Product",
    url: "Product/{id}",
    defaults: new { controller = "Product", action = "Index" },
    constraints: new { id = "\\d+" }
);

            routes.MapRoute(
                name: "ProductAction",
                url: "Product/storeProductInSession",
                defaults: new { controller = "Product", action = "storeProductInSession" }
            );

            routes.MapRoute(
                name: "Categorie",
                url: "Categorie/{id}",
                defaults: new { controller = "Categorie", action = "Index" }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Welkom", action = "Index", id = UrlParameter.Optional }
                );


        }
Donal

You should use the URL helper:

url: '@Url.Action("storeProductInSession", "Product")',

For more information, see here.

OK, I think the issue with your product object. It is not an object, rather a string. The ' characters around your product object are the issue - JavaScript thinks it is a string - not an object. It should be something like this:

var product = {
            "id": "@Model.ID",
            "naam": "@Model.Naam",
            "afbeelding": "@Model.Afbeelding",
            "beschrijving": "@Model.Beschrijving",
            "prijs": "@Model.Prijs",
            "aantal": " + amount + "
        };

See here: http://jsfiddle.net/donal/mo776xe0/1/

Also, if you know id will be an integer, you can do this:

var product = {
            "id": @Model.ID,
            "naam": "@Model.Naam",
            "afbeelding": "@Model.Afbeelding",
            "beschrijving": "@Model.Beschrijving",
            "prijs": "@Model.Prijs",
            "aantal": " + amount + "
        };

Can you try this:

[HttpPost]
public string storeProductInSession(object product)
{
    return "This method is going to do session stuff, but for now it just has to return this string";

}

Looking at your routes, I can see the problem.

It is assuming storeProductInSession is the id of the Product you are looking for and therefore going to the Index action of the Product controller.

What we need is a constraint. A constraint will tell the route to only use it if the id is a number (digit). For example: constraints: new { id = @"\d+" }

Therefore, you need to change the routes to this (You also need a route for the Product/storeProductInSession action):

routes.MapRoute(
    name: "Product",
    url: "Product/{id}",
    defaults: new { controller = "Product", action = "Index" },
    constraints: new { id = "\\d+" }
);

routes.MapRoute(
    name: "ProductAction", 
    url: "Product/storeProductInSession", 
    defaults: new {controller = "Product", action = "storeProductInSession"}
);

routes.MapRoute(
    name: "Categorie",
    url: "Categorie/{id}",
    defaults: new { controller = "Categorie", action = "Index" }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Welkom", action = "Index", id = UrlParameter.Optional }
    );

Therefore, when you are asking for Product/storeProductInSession. It will see that storeProductInSession is not a digit and then move on to the next route.

The next route will point it at the Product controller and the storeProductInSession action.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jquery Ajax call doesn't hit the action method/returns entire HTML page

From Dev

Wordpress ajax call returns entire html page

From Dev

call JQuery ajax at intervals doesn't redirect page

From Dev

AJAX call to Spring MVC works, but doesn't hit success function

From Dev

i need to return an entire html page using an ajax call

From Dev

jQuery ajax call only from HTML page

From Dev

Jquery ajax call doesn't work on mobile

From Dev

Call master page "CS" method using JQuery (ajax doesn't work)

From Dev

jquery datepicker doesn't work after ajax call if its already on page

From Dev

JQuery Ajax doesn't load a page

From Dev

Ajax doesn't refresh html page

From Dev

Why doesn't the action method accept a parameters from the ajax call?

From Dev

My AJAX call won't work on next page untill I hit refresh

From Dev

My AJAX call won't work on next page untill I hit refresh

From Dev

(Django) AJAX call doesn't hit view, form submit does. Why?

From Dev

jQuery AJAX GET to a Servlet doesn't show the JSP page - instead returns with HTML code

From Dev

Ajax Call using Jquery in HTML and take a response to other Html Page

From Dev

How can I replace the entire HTML of a page, or its body content, via an AJAX call's success callback?

From Dev

jQuery AJAX call returns entire PHP file

From Dev

Jquery function doesn't work after Ajax call

From Dev

Jquery AJAX doesn't call a code behind method

From Dev

ajax success error function doesn't call up in jquery

From Dev

Jquery/Javascript doesn't reload after ajax page change

From Dev

Ajax request in MVC doesn't return corresponding value to the HTML page

From Dev

Ajax request in MVC doesn't return corresponding value to the HTML page

From Dev

Ajax request to server from html page doesn't work

From Dev

Header doesn't go across entire page

From Dev

jquery ajax call reload page

From Dev

Struts2 jquery Plugin responds to ajax requests with HTML of the entire page

Related Related

  1. 1

    Jquery Ajax call doesn't hit the action method/returns entire HTML page

  2. 2

    Wordpress ajax call returns entire html page

  3. 3

    call JQuery ajax at intervals doesn't redirect page

  4. 4

    AJAX call to Spring MVC works, but doesn't hit success function

  5. 5

    i need to return an entire html page using an ajax call

  6. 6

    jQuery ajax call only from HTML page

  7. 7

    Jquery ajax call doesn't work on mobile

  8. 8

    Call master page "CS" method using JQuery (ajax doesn't work)

  9. 9

    jquery datepicker doesn't work after ajax call if its already on page

  10. 10

    JQuery Ajax doesn't load a page

  11. 11

    Ajax doesn't refresh html page

  12. 12

    Why doesn't the action method accept a parameters from the ajax call?

  13. 13

    My AJAX call won't work on next page untill I hit refresh

  14. 14

    My AJAX call won't work on next page untill I hit refresh

  15. 15

    (Django) AJAX call doesn't hit view, form submit does. Why?

  16. 16

    jQuery AJAX GET to a Servlet doesn't show the JSP page - instead returns with HTML code

  17. 17

    Ajax Call using Jquery in HTML and take a response to other Html Page

  18. 18

    How can I replace the entire HTML of a page, or its body content, via an AJAX call's success callback?

  19. 19

    jQuery AJAX call returns entire PHP file

  20. 20

    Jquery function doesn't work after Ajax call

  21. 21

    Jquery AJAX doesn't call a code behind method

  22. 22

    ajax success error function doesn't call up in jquery

  23. 23

    Jquery/Javascript doesn't reload after ajax page change

  24. 24

    Ajax request in MVC doesn't return corresponding value to the HTML page

  25. 25

    Ajax request in MVC doesn't return corresponding value to the HTML page

  26. 26

    Ajax request to server from html page doesn't work

  27. 27

    Header doesn't go across entire page

  28. 28

    jquery ajax call reload page

  29. 29

    Struts2 jquery Plugin responds to ajax requests with HTML of the entire page

HotTag

Archive