2012-02-04 7 views
3

ここは自分のコードです。jsonの代わりにxmlを返す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

答えて

0

おそらく、VBでは大文字と小文字を区別しないためです。

これらの行(国会議事堂のTに注意してください)試してみてください:あなたのWebサービスコードを形成し、以下の行

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

信じられないほどの迷惑な!どうもありがとうございました.. –

0

コメントを解除します。

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

は、なぜあなたはfalseからasyncを設定していますか?これにより、サーバーが応答するまでブラウザーが完全に停止します。