2012-02-04 88 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?这将完全停止浏览器,直到服务器响应。

相关问题