所以我一直在網絡上尋找一種方法將數據從我的js發送到我的控制器,並遇到了一個看似很好的解決方案。我的JSON對象結果爲null,我看不出爲什麼
function syncDB() {
if (localStorage.length != 0) {
var data = viewLog();
for (var i = 0; i < localStorage.length; i++) {
$.ajax({
url: "/Leads/RegisterOfflineEntries",
type: "POST",
dataType: "json",
data: JSON.stringify(data)
});
}
}
}
這個被髮送到:
public ActionResult RegisterOfflineEntries(string[] leads)
{
string[] splitLeads;
for (int i = 0; i < leads.Length; i++)
{
splitLeads = leads[i].Split(',');
var validFields = new List<LeadFields>();
var fields = new List<LeadFields>
{
new LeadFields() {Value = splitLeads[0], FieldsId = 1},
new LeadFields() {Value = splitLeads[1], FieldsId = 2},
new LeadFields() {Value = splitLeads[2], FieldsId = 3},
new LeadFields() {Value = splitLeads[3], FieldsId = 4},
new LeadFields() {Value = splitLeads[4], FieldsId = 5}
};
foreach (var v in fields.Where(v => !(string.IsNullOrEmpty(v.Value))))
{
v.Value = v.Value.ToUpper();
validFields.Add(v);
}
}
return RedirectToAction("Index","Home");
}
現在,不讀入代碼太多,會使問題在於ActionMethod的參數。它是空的,我不知道爲什麼,因爲當我在其他地方使用js數據時,我可以讀取它上面的數據,但將它發送到控制器不起作用。
謝謝!
編輯: 對象IM嘗試發送是從事件瀏覽器方法,看起來像這樣
function viewLog() {
var localDbList = [localStorage.length];
for (var i = 0; i < localStorage.length; i++) {
localDbList[i] = localStorage[i];
}
return localDbList;
}
我有什麼是離線支持localStorage的,我想取採取這裏存儲的值,並將其保存在我的服務器上
編輯:
Request URL:http://localhost:59848/Leads/RegisterOfflineEntries
Request Headers
Provisional headers are shown
Accept:*/*
Content-Type:json
Origin:http://localhost:59848<br>
Referer:http://localhost:59848/Leads/Register
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
X-DevTools-Emulate-Network-Conditions-Client-Id:C8AD858A-EBA0-4A75-8D89-38956182F914
X-Requested-With:XMLHttpRequest
Request Payloadview source
[DAVE, DAVID, [email protected], , ]
0: "DAVE"
1: "DAVID"
2: "[email protected]"
3: ""
4: ""
post/get通常期望一個'key = value'名字對提交的數據。你只是發送'value'(json字符串)。 'data:{foo:JSON.stringify(data)}',然後告訴asp代碼,你的'lead'在發佈數據中的'foo'參數中。 – 2014-10-27 14:14:03
沒有工作..我也不認爲我可以發送一個JSON到一個方法與字符串[]作爲參數。要改變這一點,並與JSON對象一起工作,也許它會工作 – David 2014-10-27 14:17:54