Action代码:
- Java code
package com.ktv.action;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import SongServiceImpl.SongServiceImpl;import com.ktv.common.action.KtvAction;import com.ktv.entity.Song;import com.ktv.service.SongService;public class SongAction extends KtvAction { private SongService songservice; private List<Song> songs = new ArrayList<Song>(); public SongAction(){ songservice = new SongServiceImpl(); } public String songList(){ String songname = super.getParameter("songname"); songs = songservice.getSongsBySingeralias(songname); System.out.println(songname); System.out.println(songs.get(0).getName()); return "songlist"; } //getter and setter public List<Song> getSongs() { return songs; } public void setSongs(List<Song> songs) { this.songs = songs; } }
js代码:
- JScript code
$(function(){ $("#btn1").click(function(){ var songname = $("#songnameform").val(); var data = {"songname":songname} var url = "/ktv/song/song!songList.action"; $.post(url,data,function(json){ var songlist = json.songs; alert(songlist); },"json"); }); });
XML代码:
- XML code
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts> <package name="song" namespace="/song" extends="json-default"> <action name="song" class="com.ktv.action.SongAction"> <result name="songlist" type="json"/> </action> </package></struts>
------解决方案--------------------------------------------------------
你进action方法了吗
------解决方案--------------------------------------------------------
地址栏直接输入 /ktv/song/song!songList.action 你的ajax请求,看看有没有json返回
------解决方案--------------------------------------------------------
你把双引号改成单引号试下
------解决方案--------------------------------------------------------
JSONArray js =JSONArray.fromObject(songs);
out.write(js.toString()); 貌似你该申请个输出流输出一下吧 前台才能得到 在后台方法中写
------解决方案--------------------------------------------------------
返回结果有封装json格式的数据吗?
JSONObject json=new JSONObject();
json.put("status", 0);
json.put("songname", songname);
- Java code
public String songList(){ String songname = super.getParameter("songname"); songs = songservice.getSongsBySingeralias(songname); System.out.println(songname); System.out.println(songs.get(0).getName()); return "songlist"; }