0

I have this snippet of code:

var ShopLogicOptions = {};
ShopLogicOptions.params = {orderId: "'" + JS_OrderNo + "'", cartItems:[JS_arrCartItems], subTotal: "'" + JS_SubTotal + "'",...

How do I correctly put variable JS_OrderNo in the position it is at?

The other server is receiving this: "'9134832'",

And it should be this: '9134832',

EDIT: When I document.write JS_OrderNO it looks like this 9134832, so I have to add the single quotes.

5
  • You don't have to add the single quotes. It looks like that because it's a number. Heck, document.write('abc'), you don't see quotes there either. The quotes are only needed when making string literals. "'" + JS_OrderNo + "'". This literally makes the string '9134832' (the quotes are part of the string). You don't need to add quotes. Commented Jun 26, 2012 at 19:39
  • I didn't understand your edit at all! why do you need to quote? Commented Jun 26, 2012 at 19:49
  • I am sorry if I wasn't clear. The reason is because that is how shop logic wants them, all variables can be treated as strings. This is just a tracking app. Commented Jun 26, 2012 at 19:56
  • @JoJo: All variables can be treated as strings without any processing by you. Just assume it's a string, and it should just work. Commented Jun 26, 2012 at 20:11
  • 1
    @Rocket. But if a function expect to get strings ONLY, (for some unknown reason) numbers won't do. you can simply parse any variable to string using ""+ variable or String(variable) Commented Jun 26, 2012 at 20:25

2 Answers 2

3

Remove the quotes.

var ShopLogicOptions = {};
ShopLogicOptions.params = {
    orderId: JS_OrderNo,
    cartItems: [JS_arrCartItems],
    subTotal: JS_SubTotal,
    ...
};

If you really need a string instead of a number, use this:

orderId: String(JS_OrderNo),
Sign up to request clarification or add additional context in comments.

3 Comments

Maybe this is what he should have asked, but he's asking for a string.
Thanks, Are the single apostrophes automatically added then?
He didn't mention that he needs a string. My guess is that he simply doesn't want quotes in the value. @JoJo: Try it without converting to a string. Only if that doesn't work change the code to create a string. Then the value would be enclosed in quotation marks when converting it to JSON.
1

remove the single quote

{orderId: "" + JS_OrderNo  

Comments

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.