2

I am trying to send the null data by JQuery AJAX. However, I end up getting an error message which says "Bad Request". I have tried using the word "null" to send to the database, however, this has not worked. The database does allow for nulls. I can manually add data directly into the database while inputting no data in the null columns.

                $.ajax({
                method: 'POST',
                url: 'http://localhost:58371/api/ScheduleDateAndTime',
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({
                    "startDateAndTime": startTotalTimeAndDate,
                    "endDateAndTime": endTotalTimeAndDate,
                    "studentId": studentIdSelected,
                    "staffId": staffIdSelected
                }),
                success: function (data) {
                    alert("You have assigned staff member " + staffSelected + " to student " + studentSelected + " on " +  datePickerValue   + " starting at " + startTime + " and ending at "  + endTime);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(thrownError + " have you checked that you have selected a date in the calander?\n Please ensure all fields have been correctly entered");
                }
            });  

I want to be able to send null data on the "studentId" column in the database. "studentId" is a variable that gets its data from a drop-down list, where one of the available options is "none", which has "null" as its id.

3
  • If you're getting a 400 response when you send null for that value, then it sounds like your ASP logic is not configured correctly to allow nulls. In that case you would need to amend the ASP code, not your JS. Also, be sure that you're sending an actual null value, not the word in a string, eg 'null'. Commented Aug 10, 2019 at 16:31
  • I just tried it via postman REST client and it didn't work, so it seems you're right that the problem is in the asp.net logic. I have allowed nullable in the model, here is that code "public int? staffId { get; set; } ", however, the connection is done by api. I have added the api code as well Commented Aug 10, 2019 at 16:48
  • Glad you got it sorted Commented Aug 10, 2019 at 17:25

2 Answers 2

3

The ASP.NET DTO model did not allow for nulls, once this was changed to the following I was then able to upload nulls

public int? studentId { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

1

Dont send the value that should be null

var DataToSend = {};
if(startTotalTimeAndDate != undefined)
   DataToSend.startDateAndTime= startTotalTimeAndDate;
if(endTotalTimeAndDate!= undefined)
   DataToSend.endDateAndTime= endTotalTimeAndDate;
if(studentIdSelected!= undefined)
   DataToSend.studentId= studentIdSelected;
if(staffIdSelected!= undefined)
   DataToSend.staffId= staffIdSelected;

$.ajax({
                method: 'POST',
                url: 'http://localhost:58371/api/ScheduleDateAndTime',
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(DataToSend),
                success: function (data) {
                    alert("You have assigned staff member " + staffSelected + " to student " + studentSelected + " on " +  datePickerValue   + " starting at " + startTime + " and ending at "  + endTime);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(thrownError + " have you checked that you have selected a date in the calander?\n Please ensure all fields have been correctly entered");
                }
            });  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.