1

I want to post a variable through AJAX.

The typeof variable is "object".

If I directly post an object, the AJAX fails. I used JSON.stringify but then it’s in ["10","11","12"] format.

I need a string similar to 10,11,12. How do I do that?

2
  • 1
    What is your variable? What does this object contain? If it is an array and you want to convert it to comma-separated string, use Array.prototype.join. If it is an object, then is it keys or values or what? Show some code. Commented Aug 17, 2016 at 4:29
  • 1
    Do you have some code that you have written. If yes then post here. Commented Aug 17, 2016 at 4:29

3 Answers 3

3

Just call method join on your array like this :

console.log(["10","11","12"].join(",")); // 10,11,12
Sign up to request clarification or add additional context in comments.

Comments

0

The String() function converts the value of an object to a string. The String() function returns the same value as toString() of the individual objects.

function myFunction() {
    var x1 = ["10","11","12"];
    var res = String(x1) + "<br>";
    document.getElementById("demo").innerHTML = res;
}
<p>Click the button to convert object to string.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

Comments

0

Seems like this was an array so toString() should do it. but ["10","11","12"] was the correct representation of the javascript object. [] tells it was an array, that information is permanently lost when you generate something like 10,11,12

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.