0

Im calling an api and i get an array with multiple arrays. So i want to do each of the arrays and pick up the value number 3 of each array and send that value to my controller method called addGame. If i debugg it i can se that it picks the right value each time it loops the each loop and it goes to my controller method but the value it sends is null. Here is my code:

  var date = "02/13/2014";

$.ajax({
    dataType: "jsonp",
    type: "post",
    crossDomain: true,
    url: 'http://stats.nba.com/stats/scoreboard/?LeagueID=00&gameDate=' + date + '&DayOffset=0',
    success: function (val) {
        var result = val.resultSets[0].rowSet;
        $.each(result, function (key, value) {
            var gameID = this[2];
            $.ajax({
                async: false,
                type: "post",
                url: "/Stats/addGame",
                data: gameID,
            });
        });
    }

})

And here is my controller:

  public void addGame(string gameid) 
    {

    }
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Daniel Gustafsson
  • 1,725
  • 7
  • 37
  • 78

2 Answers2

0

I think your second ajax data needs to be gameid or your c# method needs to be gameID or you need to pass it in as an object, like {gameid: gameID}, which is why you're getting null. You're passing in gameID, not gameid. Either way, it's good practice.

So:

$.ajax({
    url: "/Stats/addGame",
    dataType: 'json',
    data: { gameID: gameID },
    type: "POST",
    async: false,
    success: function (data) {},
    error: function (jqXHR, err) {}
}

And

public void addGame(string gameID) 
{

}
maggiekh
  • 204
  • 1
  • 9
  • I always over format my ajax, so you might be missing an option that I'm not sure if it is really an option or not. I'll edit my answer's example since comments don't really have formatting – maggiekh May 02 '14 at 15:23
  • Also, have you checked to see what is being passed? In Chrome you can view the the network tab in the developer tools (f12 as you're looking at the page) and it will tell you what was in the form data. I think it will be under Form Data, so you can see if it's being passed in that way, at least. – maggiekh May 02 '14 at 15:27
  • So now i come to the error and err Says parseerror, how do i parse it right? So it can't be a var in jquery and in C# controller be a string? Or where is the parseerror? @maggiekh – Daniel Gustafsson May 02 '14 at 15:30
  • right, so with jquery you can parse your objects like JSON.stringify(YourVariableHere), which would give you something like "{"gameID":"515646"}". You can unstringify (if you want to pass something back later) with JSON.parse(YourVariableHere) which gets you back {gameID: 515646} or whatever you return. – maggiekh May 02 '14 at 15:34
  • ive now added the JSON.stringify(gameID) but it still got the parseerror. Then i read that i could get parseerror when defining the dataType as json since it is only a string. So i removed it and now i doesn't go to the error in the ajaxpost and say parserror it goes to success but the success is "" and the gameID in my C# controller method is null – Daniel Gustafsson May 02 '14 at 15:41
  • It's going to success because your controller isn't doing anything, so it's returning 200. If you tried to do something (like look up the game in a db based on the ID) it would give you a null reference error or something. can you try a console.log on your gameID (so in your success or before the ajax put console.log(gameID)) to see if that's being set correctly and in a good format? – maggiekh May 02 '14 at 15:44
  • ive added console log before ajaxpost and in success and it give me gameID is the same. – Daniel Gustafsson May 02 '14 at 15:52
  • they both have the type string. :S – Daniel Gustafsson May 02 '14 at 15:55
  • try using .done instead of success, so like $.ajax({}).done(function(){$.ajax(gameID stuff)}). another thing to try is changing it from 'post' to 'get'. The only thing I can really think is causing issue is the ajax within an ajax. – maggiekh May 02 '14 at 16:09
0

I would refer to this question for the best way to identify what the variable contents are, and then I would do one of two things:

1) Convert the data coming from the web to a string before sending it to the controller

-or-

2) Change the controller function to expect the data type that is incoming from the web

Judging by your AJAX call it looks like you are getting JSON in return. You may want to parse that before sending it to your controller. See this question.

Community
  • 1
  • 1
armadadrive
  • 963
  • 2
  • 11
  • 42