###前提・実現したいこと
ajaxでサーバー側から複数の配列を取得したいです。
お分かりの方がいらっしゃれば、回答よろしくお願い致します。
###発生している問題・エラーメッセージ
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 2 column 1 of the JSON data
###該当のソースコード
javascript
1$.ajax({ 2 type: "POST", 3 url : "http://localhost:8080/PosSystem/AllSuggestServlet", 4 contentType: "application/x-www-form-urlencoded; charset=UTF-8", 5 timeout:10000, 6 }).done(function(data){ 7 var suggest = JSON.parse(data); 8 var suggestion = eval(suggest); 9 for (var i = 0; i < suggestion.length-1; i++) { 10 var name = suggestion[i][4] 11 var price = suggestion[i][9] 12 price = price.replace( /(\d)(?=(\d\d\d)+(?!\d))/g, '$1,' ); 13 var pic = suggestion[i][10] 14 $("#popularTable").append( 15 $("<tr></tr>") 16 .append($('<td><img src="'+pic+'" alt="画像を表示できません" width="60px" height="50px"></td>')) 17 .append($('<td></td>').text(name)) 18 .append($('<td><BUTTON>詳細</BUTTON></td>')) 19 ); 20 $('td').eq(0).addClass('pic'); 21 $('td').eq(1).addClass('productname'); 22 $('#popularTable BUTTON').addClass('detail'); 23 24 $("#modalDiv").append($('<div></div>') 25 .append($('<BUTTON></BUTTON>')) 26 .append($('<h1>詳細</h1>')) 27 .append($('<img src="'+pic+'" alt="画像を表示できません" width="260px" height="250px"></td>')) 28 .append($('<h2></h2>').text(name)) 29 .append($('<table></table>') 30 .append($('<tr></tr>') 31 .append($('<th></th>').text("取扱い企業名")) 32 .append($('<th></th>').text("価格")) 33 .append($('<th></th>').text("数量")) 34 .append($('<th></th>').text("発送日")) 35 .append($('<th></th>').text("発注")) 36 ) 37 .append($('<tr></tr>') 38 .append($('<td></td>').text("株式会社あいうえお")) 39 .append($('<td></td>').text('\\'+price)) 40 .append($('<td></td>').text('3つ')) 41 .append($('<td></td>').text('3日')) 42 .append($('<td><BUTTON>発注する</BUTTON></td>')) 43 )) 44 .append($('<BUTTON>すべてキャンセルする</BUTTON>')) 45 .append($('<BUTTON>確定する</BUTTON>')) 46 ); 47 $("#modalDiv div").eq(i).addClass('remodal');//一つ目にしかかからない理由は一つ目のテーブルにしかクラスdata属性をつけてないから 48 $("#modalDiv div").eq(i).attr({'data-remodal-id': 'modal'+i,'data-remodal-options': 'hashTracking:false'}); 49 $("#modalDiv BUTTON").eq(0).addClass('remodal-close'); 50 $("#modalDiv BUTTON").eq(0).attr({'data-remodal-action': 'close',}); 51 $("#modalDiv BUTTON").eq(1).addClass('dicision'); 52 $("#modalDiv BUTTON").eq(2).addClass('remodal-cancel'); 53 $("#modalDiv BUTTON").eq(2).attr({'data-remodal-action': 'cancel',}); 54 $("#modalDiv BUTTON").eq(3).addClass('remodal-confirm'); 55 $("#modalDiv BUTTON").eq(3).attr({'data-remodal-action': 'confirm',}); 56 $("#modalDiv BUTTON").eq(4).addClass('remodal-close'); 57 $("#modalDiv BUTTON").eq(4).attr({'data-remodal-action': 'close',}); 58 $("#modalDiv BUTTON").eq(5).addClass('dicision'); 59 $("#modalDiv BUTTON").eq(6).addClass('remodal-cancel'); 60 $("#modalDiv BUTTON").eq(6).attr({'data-remodal-action': 'cancel',}); 61 $("#modalDiv BUTTON").eq(7).addClass('remodal-confirm'); 62 $("#modalDiv BUTTON").eq(7).attr({'data-remodal-action': 'confirm',}); 63 $("#modalDiv BUTTON").eq(8).addClass('remodal-close'); 64 $("#modalDiv BUTTON").eq(8).attr({'data-remodal-action': 'close',}); 65 $("#modalDiv BUTTON").eq(9).addClass('dicision'); 66 $("#modalDiv BUTTON").eq(10).addClass('remodal-cancel'); 67 $("#modalDiv BUTTON").eq(10).attr({'data-remodal-action': 'cancel',}); 68 $("#modalDiv BUTTON").eq(11).addClass('remodal-confirm'); 69 $("#modalDiv BUTTON").eq(11).attr({'data-remodal-action': 'confirm',}); 70 $("#modalDiv table").addClass('toriatukai'); 71 } 72 }).fail(function(data){});
java
1package suggestion; 2 3import java.io.IOException; 4import java.io.PrintWriter; 5import java.util.ArrayList; 6import java.util.Collections; 7import java.util.LinkedList; 8 9import javax.servlet.ServletException; 10import javax.servlet.annotation.WebServlet; 11import javax.servlet.http.HttpServlet; 12import javax.servlet.http.HttpServletRequest; 13import javax.servlet.http.HttpServletResponse; 14import javax.servlet.http.HttpSession; 15 16import com.google.gson.Gson; 17 18import myclass.WineCategory; 19 20/** 21 * Servlet implementation class ComprehensiveProposal 22 */ 23@WebServlet(asyncSupported = true, urlPatterns = { "/AllSuggestServlet" }) 24public class AllSuggestServlet extends HttpServlet { 25 private static final long serialVersionUID = 1L; 26 /** 27 * 目的:一番売れているワインの色を調べ、その属性を分析する 28 * 1.一番売れている商品群を調べる 29 * 2.一番売れているカテゴリーを変数へ代入 30 * 3.その変数の詳細を分析し、どの属性(重さと味)が一番売れているか調べる 31 */ 32 33 /** 34 * @see HttpServlet#HttpServlet() 35 */ 36 public AllSuggestServlet() { 37 super(); 38 // TODO Auto-generated constructor stub 39 } 40 41 /** 42 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 43 */ 44 @SuppressWarnings({ "unchecked", "unused" }) 45 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 46 int red = 0,white = 0,rose = 0,spaW = 0,spaR = 0,other = 0,maxQuantity = 0,bestLiquors = 0,bestWeightFlavorCate = 0; 47 int intFullSweet = 0,intFullSpicy = 0,intMidSweet = 0,intMidSpicy = 0,intLightSweet = 0,intLightSpicy =0; 48 String maxLiquor = null,maxWeight = null,maxFlavor = null; 49 ArrayList<Integer> arrayLiquor= new ArrayList<Integer>(),arrayDetail = new ArrayList<Integer>(); 50 51 HttpSession session = request.getSession(true); 52 53 LinkedList<LinkedList<String>> saleTbl = 54 new LinkedList<LinkedList<String>>(); 55 saleTbl = (LinkedList<LinkedList<String>>) session.getAttribute("saleTbl");//取れてる 56 57 WineCategory wc = new WineCategory(); 58 /*WineCategory{redWine,whiteWine,rose,sparklingWhite,sparklingRed,other}*/ 59 arrayLiquor = wc.saleCate(saleTbl); 60 red = arrayLiquor.get(0); 61 white = arrayLiquor.get(1); 62 rose = arrayLiquor.get(2); 63 spaW = arrayLiquor.get(3); 64 spaR = arrayLiquor.get(4); 65 other = arrayLiquor.get(5); 66 67 /*一番売れているワインのカテゴリーを変数へ代入する*/ 68 Collections.sort(arrayLiquor); 69 /*一番売れているワインの色を取得*/ 70 bestLiquors = arrayLiquor.get(arrayLiquor.size()-1); 71 /*一番売れている商品のリキュールIDを取得*/ 72 if(bestLiquors == red){ 73 maxLiquor ="1"; 74 }else if(bestLiquors == white){ 75 maxLiquor ="2"; 76 }else if(bestLiquors == rose){ 77 maxLiquor ="3"; 78 }else if(bestLiquors == spaW){ 79 maxLiquor ="4"; 80 }else if(bestLiquors == spaR){ 81 maxLiquor ="5"; 82 }else if(bestLiquors == other){ 83 maxLiquor ="6"; 84 } 85 86 LinkedList<LinkedList<String>> suggestCampanyTbl = new LinkedList<LinkedList<String>>(); 87 suggestCampanyTbl = (LinkedList<LinkedList<String>>) session.getAttribute("suggestProductTbl"); 88 response.setContentType("text/html; charset=UTF-8"); 89 PrintWriter out = response.getWriter(); 90 Gson gson = new Gson(); 91 response.setHeader("Access-Control-Allow-Origin", "*");//これもないとダメかも 92 out.println(gson.toJson(suggestionTbl));//1つ目の配列 93 out.println(gson.toJson(suggestCampanyTbl));//2つ目の配列 94 out.close(); 95 } 96}
###試したこと
ajax側の処理doneの引数を複数選択すると2つ目のの引数にはsuccessという文字が入っている。
そして、一つ目の引数に二つの配列が格納されているので、このエラーが発生している気がする。
###補足情報(言語/FW/ツール等のバージョンなど)
eclipse 4.5
windows10
jquery1.x系
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。