我有一个工厂,其中包含下面给出的函数:
TestOperation: function(filename) {
var senddata = {
filename: filename,
operation: 'download',
base: '3',
};
return $http({
method: 'POST',
url: 'http://' + location.host + '/FtpHandler.ashx',
data: senddata
});
}
我使用它是为了让我的 FtpHandler
类从我的 ftp 服务器下载并保存文件。
类看起来像这样
FtpHandler.ashx
<%@ WebHandler Language="C#" CodeBehind="FtpHandler.ashx.cs" Class="TestWebApplication.FtpHandler" %>
FtpHandler.ashx.cs
namespace TestWebApplication
{
public class FtpHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var operationType = context.Request.Form["operation"];
System.Diagnostics.Debug.WriteLine(operationType);
...
这最终会在我的输出控制台中打印一个空行。 为什么我无法正确获取此(或任何其他)变量的值?
最佳答案
经过几次尝试,您似乎需要利用 Params
才能从请求中获取数据。
完整代码为:context.Request.Params["operation"];
您的 Request.Form
解析将为 null,因为您没有发布表单,甚至也没有在客户端上处理表单。您正在处理一个 JavaScript 对象。
我知道 Request.Params
允许您访问请求的键/值对,无论它是查询字符串还是表单集合。
至于为什么通过 Angular 使用 $http
模块不起作用,我认为您的 HttpHandler
需要一个可能无法满足的特定请求设置..但是,我再次不确定。如果您有兴趣,可以尝试以不同的方式设置您的请求:
var req = {
method: 'POST',
url: 'http://example.com',
headers: {
'Content-Type': //some content-type
},
data: { test: 'test' }
}
$http(req).then(function(){...}, function(){...});
关于javascript - 尝试在 IHttpHandler 类中读取我的 http 调用的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34359272/