I am looking to access the information stored in localstorage on the server side, but that's a story for another time.
Within my view, I have this button:
<div style="float:right;">
<input id="btnTest" type="button" name="test Json Button" value="test Json Button" onclick="sendLocalStorage();" class="btn btn-default" />
</div>
This JavaScript function is triggered by the button:
function sendLocalStorage() {
var JsonLocalStorageObj = JSON.stringify(localStorage);
$.ajax({
url: "/MyControllersName/Test",
type: "POST",
dataType: 'json',
data: JsonLocalStorageObj,
contentType: "application/json; charset=utf-8",
beforeSend: function () { alert('sending') },
success: function (result) {
alert(result.Result);
localStorage.clear();
}
});
};
This test controller method is expecting the data from the localstorage:
[HttpPost]
[WebMethod]
public ActionResult Test(string JsonLocalStorageObj)
{
string x = JsonLocalStorageObj;
//deserialize here
return RedirectToAction("Index");
}
Upon inspection using JSON.stringify(localStorage);
in chrome dev tools reveals the data as expected. However, when debugging the controller, JsonLocalStorageObj
appears to be always null
. When testing with an array of strings, it was successfully passed through. What could be causing the issue with passing JSON.stringify(localStorage);
?