0

クライアント サイトの AJAX 呼び出しを作成し、PageMethods を使用して JSON オブジェクトの応答をサーバー側に渡して条件付きロジックを実行するのに問題があります。

Web サービスから返された JSON 応答は次のとおりです。

{"Status":"Internal"}

デフォルト.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GoRedirect._Default" %>
<html>
<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">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
   <script type="text/javascript">
        function invokeService() {
            $(document).ready(function () {
                $.ajax({
                    type: "GET",
                    async: "false",
                    url: "http://Domain.Local/InternalCheck/",
                    dataType: "json",
                    success: function (result) {
                        AjaxSucceeded(result);
                    },
                    error: AjaxFailed
                });
            });
        }
        function AjaxSucceeded(result) {
            var objJSON = result.Status;
            PageMethods.GetJSONResponse(objJSON);
        }
        function AjaxFailed(result) {
            alert(result.status + ' ' + result.statusText);
        }
        invokeService();
    </script>
</form>

Default.aspx.cs

namespace GoRedirect
{
public partial class _Default : Page
{
    [Serializable]
    public class ServerJSON
    {
        public string Status { get; set; }
    }

    [WebMethod]
    public static string GetJSONResponse(string objJson)
    {
        try
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string JSONResponse = serializer.Serialize(objJson);
            return JSONResponse;
        }
        catch(Exception errorException)
        {
            return errorException.ToString();
        }

    }

    public void Page_Load(object sender, System.EventArgs e)
    {
        If (JSONResponse.Status == "Internal")
        {
        //Do something
        }
    }
}
}   
4

1 に答える 1

0

Dave Ward のこの投稿をご覧ください: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Vincent が言ったように、GET の代わりに POST を使用する必要があり、空のデータ パラメータとコンテンツ タイプが必要なようです。

$(document).ready(function () {
                $.ajax({
                    タイプ: "ポスト",
                    非同期:「偽」、
                    url: "http://Domain.Local/InternalCheck/",
                    データ型: "json",
                    データ: "{}"、
                    contentType: "application/json; charset=utf-8",
                    成功: 関数 (結果) {
                        AjaxSucceeded(結果);
                    }、
                    エラー: AjaxFailed
                });
            });

于 2012-10-30T21:54:41.040 に答える