简体   繁体   中英

Getting 500 and 404 on PUT request ASP.net

I'm really need help ! I'm trying to update some row in my DB using PUT request but it fails.

When I'm not making any change and trying to sent id to update (PUT) it returns with 404 , and when i'm making some changes it returns with 500. Anyone have an idea? Thanks

I have a table on my DB calls : "Lessons".

Here is my table from DB:

create table Lessons
(
lesson_id char(9) primary key,
lesson_name nvarchar(20) not null,
lesson_days nvarchar(50),
lesson_start_time time,
lesson_duration float(10),
capacity int check(capacity >=0),
instructor_name nvarchar(25) ,
training_type nvarchar(15) not null,
gear nvarchar(80)
);

Here is my ajax call :

$.ajax({
    dataType: "json",
    url: "/api/lesson/update",
    contentType: "application/json; charset=utf-8",
    type: "PUT",
    data: JSON.stringify(targetData),
    success: onSuccess,
    error: onError
});

while in targetData I'm sending an JavaScript Object which is contains the exact fields like my table in the DB.

Here is my PUT method from LessonController :

// PUT api/lesson/update
        [Route("api/lesson/update/")]
        public HttpResponseMessage Put([FromBody]Lesson updated_lesson)
        {
            bool succeed = new Lesson().UpdateLesson(updated_lesson); // Holds true if the lesson updated successfuly in the database.
            if (succeed)
                return Request.CreateResponse(HttpStatusCode.OK, updated_lesson.lesson_name);

            return Request.CreateResponse(HttpStatusCode.NotFound, "");
        }

Here is the function UpdateLesson which belongs to the class Lesson (classLibrary EF):

public bool UpdateLesson(Lesson updated_lesson)
        {
            try
            {

                myGymDBConnection db = new myGymDBConnection();
                Lesson l = db.Lessons.Single(x => x.lesson_id.ToLower() == updated_lesson.lesson_id.ToLower());

                l.lesson_name = updated_lesson.lesson_name;
                l.lesson_days = updated_lesson.lesson_days;
                l.lesson_start_time = updated_lesson.lesson_start_time;
                l.lesson_duration = updated_lesson.lesson_duration;
                l.capacity = updated_lesson.capacity;
                l.instructor_name = updated_lesson.instructor_name;
                l.training_type = updated_lesson.training_type;
                l.gear = updated_lesson.gear;

                var new_lines = db.SaveChanges();
                if (new_lines == 0)
                    return false;
            }

            catch (Exception e)
            {
                throw e;
            }

            return true; // means that the lesson is updated successfuly in the database.
        }
$.ajax({
    dataType: "json",
    url: "/api/lesson/update",
    contentType: "application/json; charset=utf-8",
    type: "PUT",
    data: JSON.stringify(targetData),
    success: onSuccess,
    error: onError
});

data: JSON.stringify(targetData)

In this place you create string from lesson object. In Api endpoint you take an object not a string. Just try to remove this line.

If you're using an API, you should put the addres for it, like this (this may solve your 404 error):

$.ajax({
    dataType: "json",
    url: "http:localhost:5000/api/lesson/update",
    contentType: "application/json; charset=utf-8",
    type: "PUT",
    data: JSON.stringify(targetData),
    success: onSuccess,
    error: onError
});

Double check your Lesson class: it should contain at least the same properties as targetData and it should be decorated with a [DataContract] attribute. Each property should then be decorated with a [DataMember] attribute.

EDIT: also you should debug the xhr request in the Network tab of chrome's debugging window. (You can also right click the error in the console and click "Open in network panel")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM