'jQuery AJAX Not Returning Error Message with SQL Server
I'm testing jQuery AJAX to return data from a SQL Server table. In the AJAX call I have a failure function to show any potential errors in an alert box. However, when there are errors, it doesn't show the error. It just doesn't run. If I show the browser's console I can see the error, but why isn't my failure function working?
My ASP.NET Function on the 'MyPage.aspx' page. I created an error in the SQL String as an example
Public Shared Function MyFunction(RecordID As String) As String
Dim vRESULT As String = ""
cn = New SqlConnection(cs)
cn.Open()
'THIS IS WRONG AND SHOW THROW AN ERROR BECUASE IT"S MISSING 'AUTO' AT THE END
sq = "SELECT * FROM MYTABLE FOR JSON" '<--- For example sake, I left off "AUTO"
cm = New SqlCommand(sq, cn)
dr = cm.ExecuteReader
If dr.Read() Then
vRESULT = dr(0)
End If
dr.Close()
dr = Nothing
cm = Nothing
cn = Nothing
Return vRESULT
End Function
My jQuery AJAX Script. Why doesn't the failure function work?
$(document).ready(function () {
$('#button1').click(function () {
$.ajax({
type: "POST",
url: "MyPage.aspx/MyFunction",
data: '{RecordID: 1 }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, textStatus, jqXHR) {
$("#results").html(data.d);
},
failure: function (response) {
alert(response.d);
}
})
});
});
EDIT: To be clear, here is the solution suggested in the comments. You must use this instead to display the error:
error: function (response) {
alert(response.d);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|