Reputation: 199
Im trying call to an action with Ajax and when i have one item request URL going to be like :
And everything going to work, but when i have more items request URL going to be like :
http://localhost:xxxx/User/getvarelinje?items=AAAADwCZbrc=&items=AAAADwCZ0QU=
And than i will get following error :
The input is not a valid Base64 string, since it contains a character that is not a base 64 character, more than two fill characters, or an invalid character among the fill characters.
This is my Ajax:
<script>
$("#BasketClick").click(function (e) {
e.preventDefault();
var holderHTML = '';
var params = "?";
var seperator = "";
for (var i = 0; i < localStorage.length; i++) {
let key = localStorage.key(i);
params += seperator + "items=" + key;
seperator = "&";
}
$.ajax({
type: "GET",
url: "/User/getvarelinje" + params,
dataType: 'json',
success: function (values) {
console.log(values);
for (var i = 0; i < values.length; i++) {
value = values[i]
console.log(value);
if (value != null) {
holderHTML += '<li>' + value.ItemName + '</li>';
holderHTML += '<li>' + value.Description + '</li>';
}
}
$('#RMABasket').html(holderHTML);
},
})
});
This is my action :
[HttpGet]
public JsonResult getvarelinje(byte[] items) {
var _getItemsLine = db.namespace.Where(s=>s.timestamp == items)
.Select(t=>new GetItemslineVM { Description = t.Description , ItemName = t.No_ })
.ToList();
return Json(_getItemsLine, JsonRequestBehavior.AllowGet);
}
And this is my modal :
public class tablenamespace
{
[Timestamp]
public byte[] timestamp { get; set; }
}
Can anyone please help me :)
Upvotes: 0
Views: 1275
Reputation: 2264
In your ajax function, because the values will be send in url, you need to parse your parameters with function encodeURIComponent . Like this:
$("#BasketClick").click(function (e) {
e.preventDefault();
var holderHTML = '';
var params = "?";
var seperator = "";
for (var i = 0; i < localStorage.length; i++) {
let key = encodeURIComponent(localStorage.key(i));
params += seperator + "items=" + key;
seperator = "&";
}
$.ajax({
type: "GET",
url: "/User/getvarelinje" + params,
dataType: 'json',
success: function (values) {
console.log(values);
for (var i = 0; i < values.length; i++) {
value = values[i]
console.log(value);
if (value != null) {
holderHTML += '<li>' + value.ItemName + '</li>';
holderHTML += '<li>' + value.Description + '</li>';
}
}
$('#RMABasket').html(holderHTML);
},
})
});
In your controller I believe you don't want to join all base64 strings in a single one. So you need handle each one separetedly. Like this:
public JsonResult getvarelinje2(string[] items)
{
var temp = new List<byte[]>(); // Create a list for handle each array
temp.AddRange(items.Select(i => Encoding.Unicode.GetBytes(i))); // Converts each string into one byte array
// Here you needs to find if there is a better way to compare two byte arrays.
var _getItemsLine = db.namespace.Where(s => temp.Contains(s))
.ToList();
return Json(_getItemsLine, JsonRequestBehavior.AllowGet);
}
Upvotes: 1