Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal server error ajax submission

I have a function that triggers on a button click that passes the following argument: insertSongPlay(newSong.songID); when I console.log the newSong.songID I see a value of 90, which is desired.

This function is called here which runs an ajax call:

function insertSongPlay(songID)

{
    $.ajax
    ({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "/Album/InsertSongPlay/",
        data: JSON.stringify({ "songID": songID }),
        success: function(data)
        {

            console.log("submitted");
            console.log(songID);
            //TODO: Indicate Success
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            //TODO: Indicate Error
            console.log(errorThrown);
        }
    });
}

Here is the corresponding ajax url found in my AlbumController:

public JsonResult InsertSongPlay(int songID)
{
    try
    {
        EntityDataAccess.InsertSongPlay(songID);
        return Json(true);
    }
    catch(Exception ex)
    {
        return Json(ex);
    }
}

Then in my EntityDataAccess.cs the following is run from the InsertSongPlay entity data access:

public static SongPlayDaily InsertSongPlay(int songID)
{
    using(var Context = GetContext())
    {
        var currentSongPlay = Context.SongPlayDailies.FirstOrDefault(x => x.SongID == songID && x.PlayDate == DateTime.Now.Date);
        if (currentSongPlay != null)
            currentSongPlay.NumberOfPlays++;
        else
        {
            currentSongPlay = new SongPlayDaily();
            currentSongPlay.SongID = songID;
            currentSongPlay.PlayDate = DateTime.Now.Date;
            currentSongPlay.NumberOfPlays = 1;
            Context.SongPlayDailies.Add(currentSongPlay);
        }
        Context.SaveChanges();
        return currentSongPlay;
    }
}

However, my ajax call always throws an error and prints Internal Server Error to the console log. The Visual Studio debug mode indicates some sort of circular reference error. I'm not quite sure where I'm going wrong.

Thanks!

like image 261
Jordan Lewallen Avatar asked Dec 04 '25 19:12

Jordan Lewallen


1 Answers

As we figured out in comments - there are multiple problems here. First,

return Json(ex);

Is a bad idea. That will inspect all properties of exception and try to serialize them, and all properties of those properties, to json. As you see, that is not going well, and also completely unnecessary. If you want to return error response to client - prepare it (use ex.Message for example). Plus, you don't want to return sensetive information (like stack trace) to your clients on every error.

Besides that - by doing return Json() on exception - you violate HTTP conventions. Json() will return response with status code 200 OK. But in your case it's not really "ok" - so you should return another status code indicating error. If not that circular reference error - your code in ajax "error" handler would never has been triggered, even when there was really an error.

Easiest way to fix it is just remove try-catch completely and let framework handle that for you.

Second problem is

x.PlayDate == DateTime.Now.Date

filter in query. Entity Framework has problem with accessing Date property of DateTime objects and refuses to convert that into SQL query. You can either move that to another variable:

var today = DateTime.Now.Date;
Where(x => x.PlayDate == today)

or use DbFunctions.TruncateTime:

Where(x => x.PlayDate == DbFunctions.TruncateTime(DateTime.Now))

Note that those ways are not exactly equivalent. First will compare with exact date value, and second will use SQL SYSDATETIME() function. In this case that hardly matters though.

like image 103
Evk Avatar answered Dec 07 '25 08:12

Evk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!