0

I am trying to add JSON request into a HTTP POST javascript code like below. Please see the code below.

My problem is, I need to input following format JSON and send it. But, I am getting Uncaught SyntaxError: Unexpected token : Could you please advise, What is wrong formatting JSON in my code below?

Sample JSON format to be sent=> {"records":[{"value":{"foo":"bar"}}]}

Code:

function savedata() {

    var membernameStr = parseInt(document.getElementById("MemberID").value).toString();
    var data = JSON.stringify({


        "records": [["value":{"MemberID":membernameStr,"MemberName":"Anna", "AccountNo":"7623", "Address":"Peter", "AccountType":"Peter"}]]

    });
    var xhr = new XMLHttpRequest();
    xhr.addEventListener("readystatechange", function () {
        if (this.status == 200) {
            console.log(this.responseText);

            // Based on successful response, call next POST API.
            var xhr1 = new XMLHttpRequest();
            xhr1.open("POST", "http://localhost:3500");
            xhr1.send(data);

        }
    });
    xhr.open("POST", "http://localhost:8082/topics/topictest");
    xhr.setRequestHeader("Content-Type", "application/vnd.kafka.json.v2+json; charset=utf-8");
    xhr.setRequestHeader("Accept", "application/vnd.kafka.v2+json; charset=utf-8");
    xhr.send(data);
}
3
  • 1
    Missing quote. "AccountNo":"Peter, Commented Feb 2, 2018 at 5:11
  • @Stella use like this` "records": [{"value":{"MemberID":"John","MemberName":"Anna", "AccountNo":"Peter", "Address":"Peter", "AccountType":"Peter"}}]` Commented Feb 2, 2018 at 5:13
  • Sorry, please see my updated question. Issue when I'm adding data dynamically "membernameStr" Commented Feb 2, 2018 at 5:14

2 Answers 2

2

Your JSON was incorrect in the syntax, please have a look at below JSON:

{
  "records": [
    [
      {
        "value": {
          "MemberID": membernameStr ,
          "MemberName": "Anna",
          "AccountNo": "7623",
          "Address": "Peter",
          "AccountType": "Peter"
        }
      }
    ]
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

0

why did you use array within array ? you can also use as below ..

{
  "records": [
      {
        "value": {
          "MemberID": membernameStr ,
          "MemberName": "Anna",
          "AccountNo": "7623",
          "Address": "Peter",
          "AccountType": "Peter"
        }
      }
  ]
}

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.