2

When I console log my response all I get back is HTML. How do I get the player object?

Site.JS :

$(".AddToPreRank").click(function (e) {
    e.preventDefault();
    //grab id
    var id = $(this).get(0).id;
    //append player to prerank list
    $.ajax({
        url: '@Url.Action("AddToPreRank")',
        type: 'POST',
        data: { id : id },
        success: function (response) {
            console.log(response);
            alert("hello");
        }

    });
});

LeagueController.cs :

[HttpPost]
public ActionResult AddToPreRank(int id){
    Player player= new Player();
    player = db.Players.Find(id);
    return Json(player);
}

3 Answers 3

4

You're calling an ActionResult method, which will return a lot more than the JSON you're after.

Change your code to

public JsonResult AddToPreRank(int id){
        Player player= new Player();
        player = db.Players.Find(id);

        return Json(player);
    } 

You may also need to confirm that the URL being picked up in your JavaScript file is the correct one. Either the parameter isn't being passed correctly or Razor isn't recognising the @ escape character correctly.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the response. I am still getting the same result. I was also curious as to why I cannot run in debug mode to see if the Id is actually passed to the controller. When I set the breakpoint, nothing happens.
Are you using [getglimpse.com](Glimpse)? In the browser's debug console, what Controller / Action method do you see being called? What content do you see in the HTML being returned? Is it a regular page or is there odd / error content on there?
Im using chrome. Im not sure where to look for the Controller/Action method being called. The HTML being returned is the page where the button click happened.
What kind of element is .AddToPreRank and is it enclosed in a <form> element? I want to confirm you're not submitting a form at the same time as sending the Ajax request.
It is in an <a> element. It is not in a form, it is just a list of players and I want the player to appear in my prerank list when I click add.
|
1

Try using $.post to default a return as JSON.

$.post('@Url.Action("AddToPreRank")', data: { id : id },
    function (response) {
        console.log(response);
        alert("hello");
    }
});

In addition, return a JsonResult instead of an ActionResult and return player as an anonymous type.

[HttpPost]
public ActionResult AddToPreRank(int id){
    Player player= new Player();
    player = db.Players.Find(id);

    return Json(new {player});
}

1 Comment

Thanks for the response. I tried this and am still getting the same result.
0

you can use System.Runtime.Serialization.Json in web environment,or use Newtonsoft.Json(http://json.codeplex.com/) in winform environment to resolve your problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.