java - Fetching a JSON via JSP to JS -
i'm having trouble getting json string servlet, throu jsp page javascript , imported vakata/jstree.
this how current code looks like.
servlet:
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { system.out.println("du är nu doget marketdataservlet"); treebranchstringbuilder tbsb = new treebranchstringbuilder(); request.setattribute("marketgrouplistjsonstring", tbsb.gettreebranchstring(mm.getallmarketgroups(), im.getallitems())); request.getrequestdispatcher("/marketdata.jsp").forward(request, response); }
jsp/html:
<div id="market_tree_branches"> </div> <div id="hidden"><%= request.getattribute("marketgrouplistjsonstring") %></div>
javascript:
var marketnitemsstring = $("#hidden").text(); console.log(marketnitemsstring) $('#market_tree_branches').jstree({ 'core' : { 'data' : marketnitemsstring } });
string that's generated jsp looks like:
[{"id":2,"text":"blueprints","parent":"#"},{"id":204,"text":"ships","parent":"2"},{"id":209,"text":"ship equipment","parent":"2"},{"id":211,"text":"ammunition \u0026 charges","parent":"2"},{"id":357,"text":"drones","parent":"2"},{"id":943,"text":"ship modifications","parent":"2"},{"id":1041,"text":"manufacture \u0026 research","parent":"2"},{"id":1338,"text":"structures","parent":"2"},{"id":9,"text":"ship equipment","parent":"#"},{"id":10,"text":"turrets \u0026 bays","parent":"9"}]
this error i'm getting in chrome console log:
uncaught error: syntax error, unrecognized expression: [{"id":2,"text":"blueprints","parent":"#"},{"id":204,"text":"ships","parent":"2"},{"id":209,"text":"ship equipment","parent":"2"},{"id":211,"text":"ammunition \u0026 charges","parent":"2"}, ........
i'm guessing during process of sending jsp/html javascript variable goes wrong cant figure out what, since jstree cant read variable. if copy/paste entire string html 'data': works fine.
your best bet use ajax call javasscript code directly servlet. there's absolutely no reason have jsp in middle there. can this:
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { system.out.println("du är nu doget marketdataservlet"); treebranchstringbuilder tbsb = new treebranchstringbuilder(); string json = bsb.gettreebranchstring(mm.getallmarketgroups(), im.getallitems()); response.setcontenttype("application/json"); response.getwriter().println(json); }
for javascript side, depends entirely on javascript framework want use. jquery easiest starting point this.
Comments
Post a Comment