0

I am trying to customise the jquery chat bot using the sample plugin present in https://www.jqueryscript.net/form/engage-audience-conversational-chatty.html.

I am using the following code:

var tags=[];
$.ajax({
    type: "POST",
    /*method type*/
    url: "sample.jsp",
    dataType: "text",
    data: "usrname=" + $('#uname').val(),
    async: false // To push values to an array 
}) //ajax
.done(function(data) {
    alert(data); // it displayed all the content which is needed for the array correctly
    tags.push(data);
})
.fail(function(f) {
    alert("Chatbot Module fetch failed!!");
});

I am retrieving 'data' as a String from java method:

if I use following line directly into javascript function it is working fine:

function addArr() {
  tags.push({type: 'input', tag: 'text', name: 'converse', 'chat-msg': 'Hi Welcome!!'},);
}

but if I try to push the string in to array it is not working. I am framing the string content as given below:

// java code:
result = "{type: 'input', tag: 'text', name: 'converse', 'chat-msg': 'Hi Welcome!!'},"
return result;
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64

2 Answers2

0

So what you're trying to do is push a js object to an array, but you're getting a JSON string as a response? If that's the case you could change dataType to "json".

$.ajax({
    type: "POST",
    /*method type*/
    url: "sample.jsp",
    dataType: "json", // dataType -> json
    data: "usrname=" + $('#uname').val(),
    async: false // To push values to an array 
})

The other way is parsing the string on success but it's cleaner to change the dataType.

var tags=[];
$.ajax({
    type: "POST",
    /*method type*/
    url: "sample.jsp",
    dataType: "text",
    data: "usrname=" + $('#uname').val(),
    async: false // To push values to an array 
}) //ajax
.done(function(data) {
    tags.push(JSON.parse(data)); // Parse string  JSON.parse(data)
})
.fail(function(f) {
    alert("Chatbot Module fetch failed!!");
});
zorsen117
  • 1
  • 2
  • I have tried 2 nd method but it is showing error in browser console as given in earlier post – Ajanta R Feb 01 '20 at 09:50
  • I tried to use 1 st method ie: dataType: "JSON" and it worked!!!!.. Thanks a lot – Ajanta R Feb 01 '20 at 10:48
  • { type: "input", tag: "radio", name: "module", "chat-msg": "Hi Welcome " + $('#uname').val() + "!!" + " Choose any of the colour given below!!", children: [{ value: "red", text: "red", }, { value: "blue", text: "blue", }, { value: "green", text: "green", } ] }, ); //push ends. How to send array {'red','blue','green'} in the json object. I could view the question but to choose the colour, it shows undefined. – Ajanta R Feb 01 '20 at 10:56
0

I have used dataType: "json", // dataType -> json

and then I have used JSONObject and JSONArray to create the array.

enter code here

      JSONObject dao = new JSONObject();
      JSONObject dao1 = new JSONObject();
      ResultSet rs = null;
      JSONArray ja = new JSONArray();

        String modules="";
        String result ="";
        String modl="";
        String qry = "";
        qry = "SELECT color FROM allmodules";
         rs = db1.execSQL(qry);

        while( rs.next() )
        {
            modules = rs.getString("color") ;
            dao1.put("value", modules);
            dao1.put("text", modules);
            ja.add(new JSONObject(dao1));
        }

        dao.put("type","input");
        dao.put("tag", "radio");
        dao.put("name","module");
        dao.put("chat-msg", "Hi Welcome" + user + "!!" + " Choose any of the color given below!!");
        dao.put("children",ja);


    return dao.toString();