I have implemented an Ajax request on my website, and I am calling the endpoint from a webpage. It always returns 200 OK, but jQuery execut
See this. Its also similar problem. Working i tried.
Dont remove dataType: 'JSON',
Note: echo only JSON Formate in PHP file if you use only php echo your ajax code return 200
Use the following code to ensure the response is in JSON format (PHP version)...
header('Content-Type: application/json');
echo json_encode($return_vars);
exit;
I have faced this issue with an updated jQuery library. If the service method is not returning anything it means that the return type is void
.
Then in your Ajax call please mention dataType='text'
.
It will resolve the problem.
I had the same issue. My problem was my controller was returning a status code instead of JSON. Make sure that your controller returns something like:
public JsonResult ActionName(){
// Your code
return Json(new { });
}
If you always return JSON from the server (no empty responses), dataType: 'json'
should work and contentType
is not needed. However make sure the JSON output...
jQuery AJAX will throw a 'parseerror' on valid but unserialized JSON!
Try following
$.ajax({
type: 'POST',
url: 'Jqueryoperation.aspx?Operation=DeleteRow',
contentType: 'application/json; charset=utf-8',
data: { "Operation" : "DeleteRow",
"TwitterId" : 1 },
dataType: 'json',
cache: false,
success: AjaxSucceeded,
error: AjaxFailed
});
OR
$.ajax({
type: 'POST',
url: 'Jqueryoperation.aspx?Operation=DeleteRow&TwitterId=1',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
cache: false,
success: AjaxSucceeded,
error: AjaxFailed
});
Use double quotes instead of single quotes in JSON object. I think this will solve the issue.