1

I am trying to send an array from an html page to an asp proxy (to a service), but cannot get the array to work properly.

Array Type: json

Scripting: JavaScript/jquery

var order = new Array();
for (var i = 0; i < list.length; i++) {
if (list[i].included == true)
order.push({ MarketID: list[i].marketID, Crop: list[i].crop })
}

$("#orderValue").val(order.join());
...

<form action="/hd.asp" method="post" id="hdForm">
<input type="hidden" name="order" id="orderValue" />
...
</form>

Removing the array, it works properly, but the array is required property. I have tried just sending array, using .join(), and few other things, but nothing seems to be working.

2
  • Can you show the code where you serialize the array and assign it to the field? Commented May 15, 2012 at 21:12
  • Try an alert(order) right before you try and set the val to make sure order is actually an array with some stuff in it. Otherwise, please elaborate on what you mean by nothing seems to be working. The form isn't submitting? There is no value for #orderValue? Commented May 15, 2012 at 21:14

2 Answers 2

1

Try this:

$("#orderValue").val(JSON.stringify(order));

Edit.. Oops, should have put order inside of JSON.stringify() as an argument. Try it now.

Sign up to request clarification or add additional context in comments.

4 Comments

you should add the link to the JSON library that has stringify in it as OP will need to add this script. github.com/douglascrockford/JSON-js
No need - JSON is a subset of Javascript. No link required.
JSON.stringify(order) doesn't work either, example of what array looks like after use: order=%5B%7B%22MarketID%22%3A1352%2C%22Crop%22%3A4%7D%2C%7B%22MarketID%22..repeating
Hmmm... what's the output of console.log($("#orderValue").val()); after attempting to set the value of the hidden field?
0

[EDIT] c.hill got there first :$ [/EDIT]

Don't use join(), use JSON.stringify(), e.g.:

var doit = function( myArray ) {
    var arrayString = JSON.stringify( myArray );
    $('#orderValue').val( arrayString );
    // rest of code
}

3 Comments

JSON.stringify(order) doesn't work either, example of what array looks like after use: order=%5B%7B%22MarketID%22%3A1352%2C%22Crop%22%3A4%7D%2C%7B%22MarketID%22..repea‌​ting
It looks URLEncoded. Try decoding it - there's a function if you need it Here
JSON.stringify() was the correct answer, I had another issue in the asp that was preventing the flow to the wcf service, but this answer fixed the first part and let me find the second part and get it all working, thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.