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>

默认.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/

正如文森特所说,您似乎需要使用 POST 而不是 GET,以及一个空的数据参数和内容类型:

$(文档).ready(函数 () {
                $.ajax({
                    类型:“发布”,
                    异步:“假”,
                    url: "http://Domain.Local/InternalCheck/",
                    数据类型:“json”,
                    数据: ”{}”,
                    contentType: "应用程序/json; charset=utf-8",
                    成功:函数(结果){
                        AjaxSucceeded(结果);
                    },
                    错误:Ajax 失败
                });
            });

于 2012-10-30T21:54:41.040 回答