我正在使用github/fetch polyfill将一些数据发回服务器。
fetch('./PerformSearch', {
method: 'POST',
header: {
'Accept' : 'application/json',
'Content-type' : 'application/json'
},
body: JSON.stringify({
terms: this.refs.termsFilter.getSelections(),
details: this.refs.detailsFilter.getSelections(),
dateRange: {
from: new Date(),
til: new Date()
}
})
})
但是控制器不会像在表单变量中那样自动将请求的主体反序列化到参数中的数据对象。如果我使用XmlHttpRequest执行此操作,它可以工作..
var request = new XMLHttpRequest();
request.open("POST", url, true);
request.setRequestHeader("Content-type","application/json;charset=UTF-8")
...
request.send(JSON.stringify(data))
目前在控制器中接收请求的功能非常简单 - 它没有做任何事情,我只是想让它现在使用以下内容将json主体去除数据对象:
public JsonResult PerformSearch(SearchModel.SearchSelections data)
{
//data has not been serialised
Stream req = Request.InputStream;
string body = new IO.StreamReader(req).ReadToEnd;
Console.WriteLine(body); // writes a json string
}
所以是的,我可以使用json库在函数内部自行反序列化,但我宁愿找到让控制器为我做的方法。
更新:
这是我试图解除的.net对象:
class SearchModel
{
public class SearchSelections
{
public SearchTermList terms { get; set; }
public SearchDetailsList details { get; set; }
public DateRange dateRange { get; set; }
}
public class SearchTermList
{
public IEnumerable<string> exams { get; set; }
public IEnumerable<string> areas { get; set; }
public IEnumerable<string> classes { get; set; }
public IEnumerable<string> tokens { get; set; }
}
public class SearchDetailsList
{
public IEnumerable<string> studyTypes { get; set; }
public IEnumerable<string> species { get; set; }
public IEnumerable<string> strains { get; set; }
}
public class DateRange
{
public System.DateTime @from { get; set; }
public System.DateTime til { get; set; }
}
}
答案 0 :(得分:0)
愚蠢的错误:
this.selectedLanguage
应该是<div ng-controller="MultiFileUpload" ng-init="files = [{}]">
<form ng-submit="file_upload_multiple()" novalidate>
<div class="upload_fields" ng-repeat="fileData in files">
<fieldset ng-repeat="Item in FileUploadItems">
<input type="file" name="upload_file" ngf-select ng-model="fileData.file" ngf-max-size="2MB" required />
<input type="text" name="filename" ng-model="fileData.name" required/>
<select name="filetype" ng-model="fileData.type" required>
<option label="" value=""></option>
<option label="Example Document 1" value="example_doc_1">Example 1</option>
<option label="Example Document 2" value="example_doc_2">Example 2</option>
</select>
</fieldset>
</div>
<button>Save Files</button>
</div>
:
header: {'Content-type' : 'application/json'}