2011-10-12 19 views
0

IM,我使用jquery Ajax功能將數據傳遞到Web服務jQuery的誤差函數不叫asp.net使用jQuery與Asp.net Web服務

我的問題有一個表格IM是在的WebMethod時,即時通訊評論一個的SqlParameter它會給我一個例外,但這不會打電話給我的jquery onError函數,它會一直(雖然例外與否)調用OnSuccess函數

它是什麼原因?使用Newtonsoft序列化響應JSON格式

IM

請幫我 這是我的客戶端腳本

<script type="text/javascript"> 

$(document).ready(function(){ 

    var progressOptions={ 

     steps: 20, 
     stepDuration: 20, 
     max: 100, 
     showText: true, 
     textFormat: 'percentage', 
     callback: null, 
}; 
$('#Waitdialog').dialog({ 

     autoOpen:false, 
     width: 300, 
     height:150, 
     modal: true, 
     resizable: false, 
     closeOnEscape:false, 
     open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }, 
     position:"center", 
     draggable:false, 
     title:"Done", 
     close:false, 
     buttons: { 
      "Ok": function() { 
      $(this).dialog("close"); 
      } 

     } 
    }); 
$("#submit").click(function(){ 
$("#ProgressBar").progressbar({ 

     value:50, 
     steps: 20, 
     step_duration: 20, 
     max: 100, 
     height: 12, 
     showText: true, 
     textFormat: 'percentage', 
     callback: null, 

});

var name = $("#txtName").val(); 
var userID = $("#txtUserName").val(); 
var email=$("#txtEmail").val(); 
var department=$("#cmbDep").val(); 
var password1=$("#txtPassword").val(); 
var password2=$("#txtPassword").val(); 
$.ajax({ 
    async: false, 
    cache: false, 
    type: "POST", 
    url: "http://localhost:4055/ShareMe/logon.asmx/HelloWorld", 
    data: '{"name": "' + name + '", "userID": "' + userID + '","email":"'+email+'","department":"'+department+'","password1":"'+password1+'","password2":"'+password2+'"}', 
    // data:"{'funcParam':'"+$('#form1').serialize()+"'}", 
    //  data: '{"name": "' + name + '", "userID": "' + userID + '","email":"'+email+'","department":"'+department'"},' 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: OnSuccess, 
    error: OnError 

}); 

return false; 
function OnSuccess() { 

$("#Waitdialog").dialog('open'); 

    } 

    function OnError(xhr, desc, exceptionobj) { 
        alert(xhr.responseText); 
        console.log(xhr.responseText); 
        console.log(desc); 
        console.log(exceptionobj); 

       } 
    }); 

}); 
</script> 

,所以這是我的ASP.net的webmethod

<WebMethod()> _ 
<Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _ 
Public Function HelloWorld(ByVal name As String, ByVal userID As String, ByVal email As String, ByVal department As String, ByVal password1 As String) As String 

    Try 
     Dim param(6) As SqlParameter 
     param(0) = New SqlParameter("@RefId", 1) 
     param(1) = New SqlParameter("@Name", name) 
     param(2) = New SqlParameter("@UserName", userID) 
     param(3) = New SqlParameter("@Email", email) 
     param(4) = New SqlParameter("@Department", department) 
     param(5) = New SqlParameter("@Password", password1) 
     param(6) = New SqlParameter("@CreatedBy", "") 

     SqlHelper.ExecuteNonQuery(connStringShareMe, Data.CommandType.StoredProcedure, "Users_Insert", param) 

     Return "Done" 
    Catch ex As Exception 

     Return JavaScriptConvert.SerializeObject(ex.Message) 
    End Try 

End Function 

回答

0

的jQuery如何知道你發回的消息是錯誤的?因爲你正在處理(也被稱爲吞嚥)的異常,你需要通過添加下面一行在你的異常處理代碼

HttpContext.Current.Response.StatusCode = 500 
+0

如何在我的方法中設置狀態碼? –

+0

正如我在答案中所說的,在catch異常塊中添加一行說「Response.StatusCode = 500」,這將設置狀態碼。 –

+0

在vb.net web服務方法不能設置Response.StatusCode = 500 –

1

這是因爲HTTP響應代碼設置爲500,告訴jQuery的這一行動失敗您網絡方法中的çatch子句。
捕獲異常並返回結果,所以客戶端不會看到異常。

解決此問題的一種方法是刪除catch子句,並允許將異常拋出到客戶端。

如果您選擇這樣做,最好的做法是將您的異常格式化爲用戶可以理解的內容,並刪除堆棧跟蹤/內部異常信息,以便用戶無法看到內部工作原理你的申請。

+0

其作品im刪除try和catch塊,但我如何刪除堆棧跟蹤/內部異常信息? –

+0

例如,您可以捕獲異常並拋出新的異常。用這種方法控制消息,新的異常不包含原始異常的堆棧跟蹤。但要注意以某種方式記錄原始異常,以便您可以準確地知道出了什麼問題。 –

相關問題