2016-06-01 13 views
2

私はVB.NET WebMethodにAJAX経由でデータをPOSTしようとしています。WebMethodにJSONデータを送信

JSON.stringify(myRows)含まれています

{ 
    "myRows":[ 
     { 
     "UniqueId":"188", 
     "Description":"hello", 
     "ClientId":"321", 
     "SecretKey":"dftete", 
     "Active":"checked", 
     "Delete":"delete icon" 
     }, 
     { 
     "UniqueId":"191", 
     "Description":"sfsss", 
     "ClientId":"fsdfs", 
     "SecretKey":"cvvcvb", 
     "Active":"unchecked", 
     "Delete":"delete icon" 
     }, 
     { 
     "UniqueId":"201", 
     "Description":"I am test singh", 
     "ClientId":"23424242", 
     "SecretKey":";kfddgdfl;ghf", 
     "Active":"unchecked", 
     "Delete":"delete icon" 
     }, 
     { 
     "UniqueId":"202", 
     "Description":"Yay mai ban ne wala hun", 
     "ClientId":"n.csdvnsssl", 
     "SecretKey":"nj.ssdnfvel,vgd", 
     "Active":"unchecked", 
     "Delete":"delete icon" 
     } 
    ] 
} 

私のAJAX呼び出しがある:

$.ajax({ 
     type: "POST", 
     url: "MyWebServiceUtilities.asmx/savesocialloginkeys", 
     data: JSON.stringify(myRows), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (response) { 
      //some code here 
     }, 
     failure: function (response) { 
      //some code here  
     }, 
     error: function (response) { 
      //some code here 

     } 
    }); 

サーバサイドのWebメソッドは、このです:

<WebMethod()> _ 
Public Function savesocialloginkeys(ByVal myrows As String) As String 
    Dim response As String = "" 
    '------------Some code here------------------------------- 
    '------------Response will be based on results as per code------- 
    Return response 
End Function 

デバッグしようとしたとき、AJAX呼び出しにエラーが表示されています!

+1

あなたが送信しているデータのタイプは 'contentType'です。この場合は次のようになります:' contentType: "charset = UTF-8" 'check http://stackoverflow.com/questions/18701282/what -is-content-type-and-ajax-request- – CMedina

+0

ようこそStackOverflowへ!デバッグ時に表示されるエラーを常に投稿して、間違っていることを理解できるようにしてください。 –

答えて

-1

あなたのようなcontentTypeのように「text/htmlの」を使用する必要がありますので、サーバーにJSONオブジェクトを送信されていません。

   $.ajax({ 
        type: "POST", 
        url: "MyWebServiceUtilities.asmx/savesocialloginkeys", 
        data: JSON.stringify(myRows), 
        contentType: "text/html; charset=utf-8", 
        dataType: "json", 
        success: function (response) { 
         //some code here 
        }, 
        failure: function (response) { 
         //some code here  
        }, 
        error: function (response) { 
         //some code here 

        } 
       }); 
+0

contentTypeを変更しましたが、まだ解決されていません。 – Sunny

+0

あなたのajaxリクエストを示すエラーを投稿できますか?何が間違っているのか理解するのに役立ちます –

0

あなたはWebMethod属性にJSONオブジェクトを送信しているので、AJAX呼び出しが失敗しているが、 WebMethodは文字列を受け入れます。 ASP.NETはスマートになり、文字列化されたJSONオブジェクトを実際のオブジェクトに変換しようとしています。したがって、WebMethodにリクエストが到達するまでには、文字列は使用できなくなります。

Public Class SocialConnectionModel 
    Public Property UniqueId As String 

    Public Property Description As String 

    Public Property ClientId As String 

    Public Property SecretKey As String 

    Public Property Active As String 

    Public Property Delete As String 
End Class 

あなたのJSONオブジェクトがmyRowsキーの下のオブジェクトの配列を含んでいるので、それは、WebMethod属性の署名が受け入れなければならないものです:

Imports System.Web.Services 
Imports System.ComponentModel 

<System.Web.Script.Services.ScriptService()> 
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _ 
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ 
<ToolboxItem(False)> _ 
Public Class MyWebServiceUtilities 
    Inherits System.Web.Services.WebService 

    <WebMethod()> 
    Public Function savesocialloginkeys(myRows As SocialConnectionModel()) As Boolean 
     ' Do something with the data 

     ' Return true if succeeded 
     Return True 
    End Function 

End Class 

はあなたのJSON構造を表す単純なオブジェクトが必要です

AJAXからこのWebMethodを呼び出しているので、<ScriptService()>行を含めて、これをまだ実行していない場合は重要です。予想通り

今すぐあなたのAJAX呼び出しが動作するはずです:

$.ajax({ 
    type: "POST", 
    url: "MyWebServiceUtilities.asmx/savesocialloginkeys", 
    data: JSON.stringify(myrows), 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (response) { 
     if (response && response.d) { 
      console.log("success"); 
     } 
    }, 
    failure: function (response) { 
     // some code here 
    }, 
    error: function (response) { 
     // some code here 
    } 
}); 

提案:適切なタイプの代わりに、すべてのプロパティの文字列を使用してJSONデータを構築するために試してみてください。たとえば、UniqueIdプロパティは整数で、Activeプロパティはbool(私は推測している)である可能性があります。あなたがする必要がある場合を除いて、すべてをstringly typedにしないでください。 :)

関連する問題