调用服务时不会触发Ajax Success事件

时间:2022-12-18 20:27:29

I am using JQuery to make a AJAX to a local service. My local service is a HttpHandler (e.g., Request.ashx). Within Request.ashx, it's responsiblity is to make a call to an external website (e.g., CallExternalWebsite()). CallExternalWebsite() uses .NET's System.Net.WebRequest() to initiate a request. When the external website is accessed, neither the success or error events are fired. (NOTE: I have also tried this a WCF service hosted in IIS. I am seeing the same results)

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

Here are two scenarios:

这有两种情况:

This scenario works:

此方案有效:

  1. In ProcessRequest(), comment out callExternalWebsite().
  2. 在ProcessRequest()中,注释掉callExternalWebsite()。

  3. For object o, intialize with data to simulate results.
  4. 对于对象o,初始化数据以模拟结果。

  5. Click on myButton
  6. 点击myButton

  7. The success event fires on the client.
  8. 成功事件在客户端触发。

  9. In Fiddler, I can see the header info. I see Json result, etc.
  10. 在Fiddler中,我可以看到标题信息。我看到Json的结果等

This scenario does not work:

此方案不起作用:

  1. In ProcessRequest(), enable the call for callExternalWebsite().
  2. 在ProcessRequest()中,启用callExternalWebsite()的调用。

  3. For object o, callExternalWebsite() will return an appropriate object.
  4. 对于对象o,callExternalWebsite()将返回一个适当的对象。

  5. Click on myButton
  6. 点击myButton

  7. The success event does not fires on the client.
  8. 成功事件不会触发客户端。

  9. In Fiddler, I can see the header info. I see Json result, etc.
  10. 在Fiddler中,我可以看到标题信息。我看到Json的结果等

  11. I know the callExternalWebsite() is working because I have it sending results to my phone.
  12. 我知道callExternalWebsite()正在运行,因为我将结果发送到手机。

To sum it up, the external http call within the HttpHandler is effecting the Ajax success event.

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

Here is a snippet from AJAX call: (I was trying different interations)

这是来自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
            });
        });
    });

In the HttpHandler Request.ashx,

在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);

}

Any thoughts?

Thanks.

Steve

1 个解决方案

#1


0  

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

如果你这样做会发生什么,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
        });
    });
});

#1


0  

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

如果你这样做会发生什么,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
        });
    });
});