0

When I try to use a code like this:

jQuery.post("http://mywebsite.com/", {
    array-key: "hello"
});

I get this error:

Uncaught SyntaxError: Unexpected token -

I tried surrounding the key in quotation marks, but nothing happened. I'd like to know if it's because of some other mistake I am making at the other website or because that's not how to include a dash in an object's key. And if it's not I'd like to know how to do it. Thanks in advance.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Nadroev
  • 363
  • 1
  • 11
  • 1
    Just FYI that's an object, not an array. Also `I tried surrounding the key in quotation marks` should have worked fine. – Rory McCrossan Dec 01 '15 at 15:52
  • Quotes around key will fix problem. That being said, I wouldn't get in the habit of using hyphens in key names. that is just bad practice. – Mike Brant Dec 01 '15 at 15:55
  • You can only use unquoted keys if the key would also work as the name of a variable. That means only letters, numbers, and underscores; if there are hyphens (dashes) or any other punctuation besides underscores, you need the quotes. – Mark Reed Dec 01 '15 at 15:55
  • 1
    @MikeBrant this looks like the client side of an API call, so Nadroev may not have control over the choice of key names.. – Mark Reed Dec 01 '15 at 15:56
  • 1
    Which also means if you want to access a property of an object you need to use `myObj['hyphenated-property]` which is ugly and should be avoided. EDIT im aware he's not consuming the object in the client but worth noting. – ste2425 Dec 01 '15 at 15:56

1 Answers1

3
jQuery.post("http://mywebsite.com/", {"array-key": "hello"});

should be correct. If this doesn't work, the issue is not with your request.

vzwick
  • 11,008
  • 5
  • 43
  • 63