2011-11-11 17 views
3

jQueryの初心者で、jQuery Ajaxがデータを返す方法がわかりません。私はjQueryを取得するAjaxがAsp.Netでデータを返す

[WebMethod(EnableSession = false)] 
protected int SignIn() 
{ 
    return 0; 
} 

以下のようないくつかのデータを取得するためにいくつかの簡単な機能を持っており、私の.aspxページに私はこの

$(document).ready(function() { 
     $("#si").click(function 
      () { 
      $.ajax({ 
       type: "POST", 
       url: "SignIn.aspx/SignIn", 
       contentType: "application/json", 
       success: function (txt) { 
        alert(txt); 
       } 
      }); 
     }); 
    }); 

を持っていますが、アラートで、私は全体SignIn.aspx(すべてのHTMLタグなどを取得しますに)。 SignIn()が返す0を警告する方法?ありがとう

+0

SignIn機能は.asmx webserviceにありますか? – tanathos

答えて

3

あなたはASPXファイルにデータを要求しています。私はそれがASMXであるべきだと思います。

私はこのすべて寄りかかったデイブ・ワードのポストチェックアウト:http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/

私はこのようなルックスを作ることができる最も簡単な例:

using System.Web.Services; 

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.Web.Script.Services.ScriptService] 
public class WebService : System.Web.Services.WebService { 
    [WebMethod] 
    public int Status(int input) { 
     return input + 1; 
    } 
} 

を含むWebサービス(ASMX)を追加します次に、あなたのHTML doで

<html> 
<head> 
    <title>Test</title> 
    <script src="jquery-1.6.2.min.js" ></script> 
</head> 
<body> 
<script> 
    $(function() { 
    $.ajax({ 
     url: 'WebService.asmx/Status', 
     data: '{ input: 0 }', 
     type: 'POST', 
     dataType: 'json', 
     contentType: 'application/json', 
     success: function (data, status) { 
     alert(data); 
     alert(typeof data); 
     } 
    }); 
    }); 
</script> 
</body> 
</html> 

ajaxでデータ内で定義された文字列は、Webメソッドへの入力です。名前は一致する必要があります。成功コールバックでは、最初のアラートは返された値(入力+1)を示し、2番目のアラートは文字列ではなく数値であることを示します。データ型はJSONに設定されているため、WebメソッドはJSONを返して、データを正しく入力できるようにします。

これが役に立ちます。

+0

ありがとう:) – ePezhman

3

このサンプルを試してみてください。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace TestProject 
{ 
    /// <summary> 
    /// Summary description for TestHandler1 
    /// </summary> 
    public class TestHandler1 : IHttpHandler 
    { 

     public void ProcessRequest(HttpContext context) 
     { 
      string id = context.Request["id"]; 
      context.Response.ContentType = "text/plain"; 
      context.Response.Write(id); 
     } 

     public bool IsReusable 
     { 
      get 
      { 
       return false; 
      } 
     } 
    } 
} 

:私は.....

ハンドラのコードをハンドラにASPXからIDを渡され、もう一度

これはサンプル例ですASPXするためにサーバー側のデータを表示するために、そこから戻ってきましたそして、ASPX

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
    </head> 
<body> 
    <form id="form1" runat="server"> 

    </form> 
    <script type="text/javascript"> 
     $(function() { 
      $.ajax({ 
       type: 'POST', 
       url: 'TestHandler.ashx?id='+ 1, 
       data: 'id=' + 1, 
       success: function (msg) { 
        alert(msg); 
       } 
      }); 

     }); 
    </script> 
</body> 
</html> 
+0

これは文字列として渡された数値を戻しますが、OPにはJSONデータが返されるように思われるので、intはintです。 – nickd

+1

@nickdオペレーションは「jQueryとjQuery Ajaxがデータを返す方法を理解していない」と述べています。このコメントのサンプルとしてこれを説明しています。 –

+0

ありがとうございます。しかし、正確には、ajaxでPOSTを使用しましたデータを送信するためにURLを使用しましたか?メソッドを検出するためのURLだけですか? – ePezhman

4

SignIn方法は、静的および公衆作成し、次のコードで警告を表示:alert(txt.d);

+0

ありがとうございます。警告欄に "undefined"が表示されます – ePezhman

+0

'alert(txt);' dのプロパティの説明はこちらhttp://あなたの努力のためにencosia.com/never-worry-about-asp-net-ajaxs-d-again/ –

関連する問題