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 executes the error event. I tried a lot of things, but I could not figure out the problem. I am adding my code below:
jQuery Code
var row = "1";
var json = "{'TwitterId':'" + row + "'}";
$.ajax({
type: 'POST',
url: 'Jqueryoperation.aspx?Operation=DeleteRow',
contentType: 'application/json; charset=utf-8',
data: json,
dataType: 'json',
cache: false,
success: AjaxSucceeded,
error: AjaxFailed
});
function AjaxSucceeded(result) {
alert("hello");
alert(result.d);
}
function AjaxFailed(result) {
alert("hello1");
alert(result.status + ' ' + result.statusText);
}
C# code for JqueryOpeartion.aspx
protected void Page_Load(object sender, EventArgs e) {
test();
}
private void test() {
Response.Write("<script language='javascript'>alert('Record Deleted');</script>");
}
I need the ("Record deleted")
string after successful deletion. I am able to delete the content, but I am not getting this message. Is this correct or am I doing anything wrong? What is the correct way to solve this issue?
You just have to remove
dataType: 'json'
from your header if your implemented Web service method is void.In this case, the Ajax call don't expect to have a JSON return datatype.
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.
Try following
OR
Use double quotes instead of single quotes in JSON object. I think this will solve the issue.
I've had some good luck with using multiple, space-separated
dataType
s (jQuery 1.5+). As in: