黑马程序员技术交流社区
标题:
ajax json 问题
[打印本页]
作者:
王建亮
时间:
2015-3-30 16:51
标题:
ajax json 问题
本帖最后由 王建亮 于 2015-3-30 17:18 编辑
在做一个三级联动的时候出了问题,求大侠解救。。。
这个是struts2的配置
<package name="struts2" namespace="/" extends="json-default">
<action name="countryAction" class="countryAction">
<result type="json"></result>
</action>
<action name="provinceAction" class="provinceAction">
<result type="json"></result>
</action>
<action name="cityAction" class="cityAction">
<result type="json"></result>
</action>
<action name="townAction" class="townAction">
<result type="json"></result>
</action>
</package>
这个是具体的Action
package com.youyou.action;
import java.util.List;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionContext;
import com.youyou.base.BaseAction;
import com.youyou.domain.City;
import com.youyou.domain.Province;
@Component
@Scope("prototype")
public class CityAction extends BaseAction<City>{
/**
*
*/
private static final long serialVersionUID = 5493014910574642405L;
private Long provinceId;
private List<City> cities;
public String getCityInfo() {
System.out.println("---》cityaction");
provinceId = 1L;
Province province = provinceService.findById(provinceId);
cities = cityService.findByProvince(province);
System.out.println("城市的个数为:"+ cities.size());
System.out.println("provinceId22="+provinceId);
return SUCCESS;
}
public Long getProvinceId() {
return provinceId;
}
public void setProvinceId(Long provinceId) {
this.provinceId = provinceId;
}
public List<City> getCities() {
return cities;
}
public void setCities(List<City> cities) {
this.cities = cities;
}
}
这个是jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript" src="style/js/jquery-1.3.1.js"></script></head>
<body>
<script type="text/javascript">
function getCity() {
var sUrl = "cityAction!getCityInfo.action";
var xmlHttp = createxmlHttp();
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4) {
alert("come back");
var data = eval("(" + xmlHttp.responseText + ")");//报错 Uncaugh SyntaxError:Unexpected token <
alert("comesss");
alert(data.cities[1].name);
for(var dex = 0 ; dex < data.cities.length ; dex++) {
var $option = $("<option></option>");
$option.attr("value", data.cities[dex].id);
$option.text(data.cities[dex].name);
$("#city").append($option);
}
}
};
xmlHttp.open('POST', sUrl,true);
xmlHttp.send(null);
}
//create xmlHttp
function createxmlHttp() {
var xmlHttp;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("您的浏览器不支持AJAX!");
}
}
}
return xmlHttp;
}
function getProvince() {
var sUrl = "provinceAction!getProvinceInfo.action";
var xmlHttp = createxmlHttp();
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4) {
var data = eval("(" + xmlHttp.responseText + ")");
for(var dex = 0 ; dex < data.provinces.length ; dex++) {
var $option = $("<option></option>");
$option.attr("value", data.provinces[dex].id);
$option.text(data.provinces[dex].name);
$("#province").append($option);
}
}
};
xmlHttp.open('POST', sUrl,true);
xmlHttp.send(null);
}
</script>
<input type="button" value="ajax"/>
<div class="d">
<select id="province"></select>
<select id="city"></select>
<select id="town"></select>
</div>
</body>
</html>问题描述:总是得不到从后台发过来的数据,,,
报错: Uncaugh SyntaxError:Unexpected token <
网上找了好多都不行。。。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2