I am trying to pass multiple values for a single field name to an ajax call:
<form>
<input type="hidden" name="fieldName" value="MyValue1">
<input type="hidden" name="fieldName" value="MyValue2">
<input type="hidden" name="fieldName" value="More data...">
<input type="hidden" name="fieldName" value="More data...">
<input type="hidden" name="fieldName" value="More data...">
<input type="hidden" name="fieldName" value="More data...">
</form>
I cannot simply submit the form because the submission has to be done using ajax fo开发者_如何学Pythonr reasons that are not relevant to this situation.
When I serialize this form data, I get the following:
formData: Object
fieldName: Array[6]
0: "MyValue1"
1: "MyValue2"
2: "More data..."
3: "More data..."
4: "More data..."
5: "More data..."
length: 6
So far, this looks good to me. Here is where I submit this to the server:
$.post("/MyHandler.axd", formData, function (data) {
// etc.
});
On the server, code which I do not control does the following:
string[] values = request.Form.GetValues("fieldName");
At this point, values
is null. However, if I do this:
string[] values = request.Form.GetValues("fieldName[]");
values
has my array of length 6, with the correct data in it, etc.
I understand what is happening here, but I don't know why or how I can get around it. Is there a way for me to pull out fieldName
instead of fieldName[]
without submitting the form?
Thanks.
EDIT: After trying @raymondralibi's suggestions, I am now getting the following:
For the first suggestion (getSerializeArray($(this).closest('form'))
), I get this:
formData: Object
: undefined
__proto__: Object
And for the second (getSerializeArray2($(this).closest('form'))
), I get this:
formData: Array[1]
0: Object
: undefined
__proto__: Object
length: 1
__proto__: Array[0]
Perhaps I am calling these incorrectly; any thoughts? Thanks again.
I hope this will help.
// $.serializeArray() return [{name:fieldname, value:MyValue1}, ...]
// this function will return {fieldname:MyValue1, ...}
var getSerializeArray = function($serialize_array){
var obj = {};
for(var i=0; i<$serialize_array.length; i++){
obj[$serialize_array[i].name] = $serialize_array[i].value;
}
return obj;
};
// and this function will return [{fieldname:MyValue1}, {fieldname:MyValue2}, ...]
var getSerializeArray2 = function($serialize_array){
var arr = [];
for(var i=0; i<$serialize_array.length; i++){
arr[i] = {};
arr[i][$serialize_array[i].name] = $serialize_array[i].value;
}
return arr;
};
// just call
getSerializeArray($('form').serializeArray());
getSerializeArray2($('form').serializeArray());
Well, I ended up getting this working, but I don't like how I did it:
Before the code that executes that calls request.Form.GetValues("fieldName")
, I do the following:
var entriesTable = typeof(System.Collections.Specialized.NameObjectCollectionBase).GetField("_entriesTable", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(HttpContext.Current.Request.Form) as System.Collections.Hashtable;
entriesTable.Add("fieldName", entriesTable["fieldName[]"]);
This copies the ArrayList to the correct key in the hash table, and everything works fine. Still, I would prefer to have this passed correctly from the client side, but for now this works.
精彩评论