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:
此方案有效:
- In ProcessRequest(), comment out callExternalWebsite().
- For object o, intialize with data to simulate results.
- Click on myButton
- The success event fires on the client.
- In Fiddler, I can see the header info. I see Json result, etc.
在ProcessRequest()中,注释掉callExternalWebsite()。
对于对象o,初始化数据以模拟结果。
点击myButton
成功事件在客户端触发。
在Fiddler中,我可以看到标题信息。我看到Json的结果等
This scenario does not work:
此方案不起作用:
- In ProcessRequest(), enable the call for callExternalWebsite().
- For object o, callExternalWebsite() will return an appropriate object.
- Click on myButton
- The success event does not fires on the client.
- In Fiddler, I can see the header info. I see Json result, etc.
- I know the callExternalWebsite() is working because I have it sending results to my phone.
在ProcessRequest()中,启用callExternalWebsite()的调用。
对于对象o,callExternalWebsite()将返回一个适当的对象。
点击myButton
成功事件不会触发客户端。
在Fiddler中,我可以看到标题信息。我看到Json的结果等
我知道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
});
});
});