0
<script>
 function editComment(id) {
 var content = $('#Content').val();
 var modelData = { 'Id': id, 'Content': content };
 $.ajax({
 type: 'POST',
 dataType: 'json', 
 url: '@Url.Action("EditC", "Comment")',
 data: JSON.stringify({ model: modelData }),
 contentType: 'application/json',
 success: function () {
 alert("YES");
 },
 error: function () {
 alert("Error");
 }
 });
 }
 </script>

Here the server is returning 200 OK, but for some reason the error function is getting called. Both type and contentType seem to be correct. Any idea how to fix this?

Edit: After adding

error: function (xhr, textStatus, error) {
 console.log(xhr.responseText);
 console.log(xhr.statusText);
 console.log(textStatus);
 console.log(error);
 }

this is what is being logged:

parsererror
parsererror
SyntaxError: Unexpected end of JSON input
 at parse (<anonymous>)
 at ajaxConvert (jquery-3.4.1.js:9013:19)
 at done (jquery-3.4.1.js:9483:15)
 at XMLHttpRequest.<anonymous> (jquery-3.4.1.js:9785:9)
asked Jul 11, 2022 at 12:00
8
  • 1
    Add a parameter to the error callback function and log that parameter to the console. (Or observe it in the browser's script debugger.) What does it tell you about the error? How have you confirmed that the server is returning a 200 response? Or that the operation isn't perhaps being invoked twice, once successfully and once failing? Commented Jul 11, 2022 at 12:02
  • 1
    It seems the JSON that is returned from your server is incomplete/invalid. The problem is most likely on the server. In the Developer Tools under the Network tab you should be able to see what your server is returning. Commented Jul 11, 2022 at 12:09
  • @Ivar What the server is returning is return new HttpStatusCodeResult(200) and under the Network tab it says 200. Commented Jul 11, 2022 at 12:11
  • 1
    @EL02: In the network tab, is there any body content in the response? Commented Jul 11, 2022 at 12:30
  • 1
    @EL02: Mostly a guess at this point, but what happens if you remove dataType: 'json', entirely and let jQuery infer the response type? Commented Jul 11, 2022 at 12:35

1 Answer 1

2

Moving to an answer for future readers...

The server is indeed returning a successful response, but an empty successful response:

return new HttpStatusCodeResult(200);

However, the client-side code is expecting valid JSON:

dataType: 'json',

As a result, after the successful response is received, internally the jQuery code attempts to parse that response as JSON and that fails, resulting in triggering the error callback.

Since there's no response body (and in most cases even when there is, as long as the server returns the correct MIME type), you don't need or want to specify a dataType in the jQuery AJAX operation. Simply remove this part:

dataType: 'json',
answered Jul 11, 2022 at 12:40
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.