1

我正在使用 JQuery 对本地服务进行 AJAX。我的本地服务是一个 HttpHandler(例如 Request.ashx)。在 Request.ashx 中,它的职责是调用外部网站(例如,CallExternalWebsite())。CallExternalWebsite() 使用 .NET 的 System.Net.WebRequest() 来发起请求。访问外部网站时,不会触发成功或错误事件。(注意:我也尝试过托管在 IIS 中的 WCF 服务。我看到了相同的结果

这里有两种情况:

此方案有效:

  1. 在 ProcessRequest() 中,注释掉callExternalWebsite()。
  2. 对于对象 o,使用数据进行初始化以模拟结果。
  3. 点击我的按钮
  4. 成功事件在客户端触发。
  5. 在 Fiddler 中,我可以看到标题信息。我看到了 Json 结果等。

这种情况不起作用:

  1. 在 ProcessRequest() 中,启用对 callExternalWebsite() 的调用。
  2. 对于对象 o,callExternalWebsite() 将返回一个适当的对象。
  3. 点击我的按钮
  4. 成功事件不会在客户端上触发。
  5. 在 Fiddler 中,我可以看到标题信息。我看到了 Json 结果等。
  6. 我知道 callExternalWebsite() 正在工作,因为我让它将结果发送到我的手机。

总而言之,HttpHandler 中的外部 http 调用正在影响 Ajax 成功事件。

这是 AJAX 调用的一个片段:(我在尝试不同的交互)

    $(document).ready(function () {
        $("#myButton").click(function (event) {

            $.ajax({
                cache: false,
                type: "POST",
                url: "http://localhost/Service/Request.ashx",
                data: '{"id" : "053252f3"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                timeout: 20000,
                success: function (msg) {
                    AjaxSucceeded(msg);
                },
                error: AjaxFailed
            });
        });
    });

在 HttpHandler Request.ashx 中,

public Void ProcessRequest(httpContent context)
{
//  Do some stuff....

// Make call to external web site
object o = callExternalWebsite (Uri, Data, "POST");

// Return results from callOtherWebsite 
        JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
        string json = javaScriptSerializer.Serialize(o);
        context.Response.ContentType = "application/json";
        context.Response.Write(json);

}

有什么想法吗?

谢谢。

史蒂夫

4

1 回答 1

0

What happens if you do this, msg vs msg.d:

$(document).ready(function () {
    $("#myButton").click(function (event) {

        $.ajax({
            cache: false,
            type: "POST",
            url: "http://localhost/Service/Request.ashx",
            data: '{"id" : "053252f3"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            timeout: 20000,
            success: function (msg) {
                AjaxSucceeded(msg.d);
            },
            error: AjaxFailed
        });
    });
});
于 2012-04-13T13:41:04.067 回答