First Last

First Last

  • NA
  • 648
  • 71.5k

Error: SyntaxError: Unexpected token < in JSON

Oct 11 2020 6:02 PM
I know why I am getting this message as the AJAX is completing and expects JSON coming back.
 
However, I coded the controller action method such that if all is good, redirects to another view. If there is an error is found, return JSON - an error message.
 
From the AJAX perspective as I am learning, it will expect JSON back regardless of a good (the redirect) or error situation (I return JSON).
 
So how do I code that up in the AJAX to recognize that no JSON was returned when it is a good situation as I do the redirect and do not programmatically return anything?
 
------------
 
The action method executes successfully in the sense that it does the delete, executes the redirect but gets caught on the return and I get the message:
 
    Error: SyntaxError: Unexpected token < in JSON 
 
------------
 
My actions method is:
 
I have
    Task
If I try
    Task
I get an error on the redirect statement. 
  1. public async Task DeleteUserProfile()  
  2.         {  
  3.             string errorMessage = String.Empty;  
  4.   
  5.             try  
  6.             {  
  7.                 Some code to call the web api...  
  8.   
  9.                 using (var client = new HttpClient())  
  10.                 {  
  11.                     Some code...  
  12.   
  13.                     // Call the web api - HTTP POST.  
  14.                     HttpResponseMessage result = await client.PostAsync(restOfUrl, argsData);  
  15.   
  16.                     if (result.IsSuccessStatusCode)  
  17.                     {  
  18.                         return RedirectToAction("Index""User");  
  19.                     }  
  20.                     else  
  21.                     {  
  22.                         // The web api sent an error response.  
  23.                         errorMessage = "Server error on deleting the user profile. Reason: " + result.ReasonPhrase;  
  24.                     }  
  25.                 }  
  26.             }  
  27.             catch (Exception ex1)  
  28.             {  
  29.                Some code...  
  30.             }  
  31.   
  32.             // Return a JSON object to show the error message.  
  33.             return Json(errorMessage, JsonRequestBehavior.AllowGet);  
  34.         }  
My view that has the AJAX that calls the method: 
  1. <input class="btn btn-danger deleteButton" value="Delete Your Profile">  
There is a Modal window and on a Yes, executes the AJAX. And I was thinking it would only come back if I pushed the JSON with the error message. 
  1. $('.btn-yes11').click(function() {  
  2.                 $('#myModal11').modal('hide');  
  3.   
  4.                 $.ajax({  
  5.                     type: 'POST',  
  6.                     url: '@Url.Action("DeleteUserProfile", "UserProfile")',  
  7.                     dataType: "json",  
  8.                     success: function(jsondata) {  
  9.                         if (jsondata.length > 0)  
  10.                         {  
  11.                             // Set the error message.  
  12.                             $("#jsonErrorMessage").text(jsondata);  
  13.                             // Show.  
  14.                             $("#jsonErrorMessage").css("display""block");  
  15.                         }  
  16.                         else  
  17.                         {  
  18.                             // Hide.  
  19.                             $("#jsonErrorMessage").css("display""none");  
  20.                         }  
  21.                      },  
  22.                     error: function(xhr, ajaxOptions, thrownError) {  
  23.                         alert('Critical Error: something is wrong in the call to DeleteUserProfile for delete! Status: ' + xhr.status + '. Error: ' + thrownError.toString() + '. Response Text: ' + xhr.responseText);  
  24.                     }  
  25.                 });  
  26.   
  27.                 return true;  
  28.             });   
 
 
 
 

Answers (4)