C# 设置响应消息内容,但我无法检索它
我有一个服务器方法,它获取并编码字符串列表,因为IE9不读取json格式,所以我想在http响应消息的内容中设置字符串。我似乎做得很好,但当我得到响应时,我无法在身体上找到响应,我能看到的只是我在响应消息中设置的长度 代码如下:C# 设置响应消息内容,但我无法检索它,c#,.net,web-services,http,asp.net-web-api,C#,.net,Web Services,Http,Asp.net Web Api,我有一个服务器方法,它获取并编码字符串列表,因为IE9不读取json格式,所以我想在http响应消息的内容中设置字符串。我似乎做得很好,但当我得到响应时,我无法在身体上找到响应,我能看到的只是我在响应消息中设置的长度 代码如下: public HttpResponseMessage Upload() { HttpResponseMessage m = new HttpResponseMessage(HttpStatusCode.OK); List<s
public HttpResponseMessage Upload()
{
HttpResponseMessage m = new HttpResponseMessage(HttpStatusCode.OK);
List<string> returnlist = new List<string>();
for (int i = 0; i < Request.Files.Count; i++)
{
//string treatment
}
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StringContent(string.Join(",", returnlist));
return response;
}
如何设置并获取响应消息
如有需要,js在此提供:
self.uploadfiles = function () {
//startSpinner();
if (!self.isOldIE()) {
var loc = window.location;
base_url = loc.protocol + "//" + loc.host;
baseUrl = base_url + "/";
var save = true;
var xhr = new XMLHttpRequest();
xhr.open('POST', baseUrl + 'Home/Upload');
xhr.send(formdata);
xhr.onreadystatechange = function (data) {
//The responseText is the same as above
if (xhr.responseText.length > 0) {
//do stuff
}
};
尝试使用Javascript获取响应:
var xhr = new XMLHttpRequest();
xhr.open('POST', baseUrl + 'Home/Upload', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
alert(xhr.responseText);
} else {
// do something about the error
}
};
xhr.send(formdata);
请注意用于异步获取的open方法的第三个布尔参数。请尝试此代码
var response = Request.CreateResponse(HttpStatusCode.OK, string.Join(",", returnlist));
return response;
由于某些原因,浏览器没有显示http响应的整个主体。因此,我的解决方案是编写响应的标题并从js获取标题。 也许有人知道为什么浏览器不显示主体,或者有更好的解决方案 这是我的控制器:
public HttpResponseMessage Upload()
{
HttpResponseMessage m = new HttpResponseMessage(HttpStatusCode.OK);
List<string> returnlist = new List<string>();
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFileBase file = Request.Files[i]; //Uploaded file
//Use the following properties to get file's name, size and MIMEType
int fileSize = file.ContentLength;
if (file.FileName != "")
{
string fileName = string.Format("{0}.{1}", Guid.NewGuid().ToString(), file.FileName.ToString().Split('.')[1].ToString());
returnlist.Add(fileName);
}
}
//Here I write the response message headers
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StringContent(string.Join(",", returnlist), Encoding.UTF8, "text/html");
response.Headers.Add("data", string.Join(",", returnlist));
return response;
}
状态说明一切。。。错误404:您请求的页面不存在。。。您在这里显示的代码可能永远不会被击中……这是因为我在js ajax中像处理json一样处理响应,我编辑了这个问题。我假设您在ApicController中工作,但听起来可能不是这样。保留您的原始代码并尝试使用新的答案来获得响应。我使用的是Controller类,但我需要的是创建一个不是json的响应以在ie9中使用,并使用js从客户端检索它。客户端已解析,但我在响应中没有看到我发送的http消息中的字符串。您还可以粘贴javascript吗?
public HttpResponseMessage Upload()
{
HttpResponseMessage m = new HttpResponseMessage(HttpStatusCode.OK);
List<string> returnlist = new List<string>();
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFileBase file = Request.Files[i]; //Uploaded file
//Use the following properties to get file's name, size and MIMEType
int fileSize = file.ContentLength;
if (file.FileName != "")
{
string fileName = string.Format("{0}.{1}", Guid.NewGuid().ToString(), file.FileName.ToString().Split('.')[1].ToString());
returnlist.Add(fileName);
}
}
//Here I write the response message headers
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StringContent(string.Join(",", returnlist), Encoding.UTF8, "text/html");
response.Headers.Add("data", string.Join(",", returnlist));
return response;
}
var xhr = new XMLHttpRequest();
xhr.open('POST', baseUrl + 'Home/Upload');
xhr.send(formdata);
xhr.onreadystatechange = function (data) {
if (xhr.responseText.length > 0) {
//header response
var response = xhr.responseText;
}