2012-02-04 86 views
3

這是我的代碼;返回xml而不是json的ASP.NET web服務

$(document).ready(function() { 
    $('#btnAddCat').click(function() { 
     var cn = $('#txtCategoryName').val(); 
     alert(cn); 
     $.ajax({ 
      type: 'Post', 
      url: "../_ws/news.asmx/InsertCategory", 
      data: { catName: + cn }, 
      contenttype: 'application/json; charset=utf-8', 
      datatype: 'json', 
      async: false, 
      success: function (msg) { 
       alert(msg); 
      }, 
      error: function (msg) { 
       alert('failure'); 
       alert(msg); 
      } 
     }); 
    }); 
}); 

,這裏是在news.asmx Web服務代碼

Imports System.Web 
Imports System.Web.Services 
Imports System.Web.Services.Protocols 
Imports System.Web.Script.Services 

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
'<System.Web.Script.Services.ScriptService()> _ 
<WebService(Namespace:="http://tempuri.org/")> _ 
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ 
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ 
Public Class News 
    Inherits System.Web.Services.WebService 


    <WebMethod()> _ 
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> 
    Public Function InsertCategory(ByVal catName As String) As String 
     Dim newid = KTOEOS.NewsCategories.InsertCategory(catName) 
     Return "{'c':" & newid & "}".ToString 
    End Function 

End Class 

返回的結果是:

<?xml version="1.0" encoding="utf-8"?> 
<string xmlns="http://tempuri.org/">{'c':21}</string> 

如果我改變了jQuery部分閱讀;

...

data: { catName: + cn }, 

...

然後我得到一個錯誤:

System.InvalidOperationException: Missing parameter: catName 

我在做什麼錯?

+0

您可以打印Web服務的入站和出站消息嗎? – Wint 2012-02-04 03:33:42

回答

0

可能是因爲您習慣了VB不區分大小寫。

嘗試這些線(注意是國會大廈T):

contentType: 'application/json; charset=utf-8', 
dataType: 'json', 
+0

令人難以置信令人難以置信和煩人!非常感謝你.. – 2012-02-04 08:27:43

0

取消對下面的線形成你的Web服務代碼。

<System.Web.Script.Services.ScriptService()> 

你爲什麼要設置asyncfalse?這將完全停止瀏覽器,直到服務器響應。

相關問題