ajax调用controller中的方法失败
js中的ajax代码$.ajax({url:"tunnelevent/reflectEventKey",type:"post",data:{type:type},succ...
js中的ajax代码
$.ajax({
url:"tunnelevent/reflectEventKey",
type:"post",
data:{type:type},
success:function(data){
var textId=$("#key");
textId.val(data);
},
cache:false,
timeout:5000,
error:function(){
alert("获取标识失败!");
subFlag=false;
}
});
controller代码
package com.wonders.hsb.event.controller;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.wonders.hsb.core.utils.ClassUtil;
import com.wonders.hsb.core.utils.DateUtil;
import com.wonders.hsb.event.constant.TunnelConsts;
import com.wonders.hsb.event.constant.TunnelEventConsts;
import com.wonders.hsb.event.services.TunnelEventService;
import com.wonders.hsb.event.services.TunnelService;
import com.wonders.hsb.event.vo.TunnelEventVo;
import com.wonders.hsb.event.vo.TunnelVo;
import com.wonders.hsb.message.utils.MessageConsts;
import com.wonders.hsb.message.vo.MessageVo;
@Controller
@RequestMapping("/tunnelevent")
public class TunnelEventController extends EventBaseController {
@RequestMapping(value = "/reflectEventKey")
@ResponseBody
public String reflectkey(String type) {
try{
System.out.println("reflect");
String property="ttt";
String key=(String) ClassUtil.finalValue(type,property);
return key;
}
catch (Exception e){
System.out.println("读取事件类型失败!");
return "";
}
}
}
没有运行到system.out.println("reflect"),为什么ajax不能调用tunnelevent/reflectEventKey?
如果为整个类定义一个@RequestMapping,然后为每个方法指定一个@RequestMapping,怎样直接写方法的url进行调用 展开
$.ajax({
url:"tunnelevent/reflectEventKey",
type:"post",
data:{type:type},
success:function(data){
var textId=$("#key");
textId.val(data);
},
cache:false,
timeout:5000,
error:function(){
alert("获取标识失败!");
subFlag=false;
}
});
controller代码
package com.wonders.hsb.event.controller;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.wonders.hsb.core.utils.ClassUtil;
import com.wonders.hsb.core.utils.DateUtil;
import com.wonders.hsb.event.constant.TunnelConsts;
import com.wonders.hsb.event.constant.TunnelEventConsts;
import com.wonders.hsb.event.services.TunnelEventService;
import com.wonders.hsb.event.services.TunnelService;
import com.wonders.hsb.event.vo.TunnelEventVo;
import com.wonders.hsb.event.vo.TunnelVo;
import com.wonders.hsb.message.utils.MessageConsts;
import com.wonders.hsb.message.vo.MessageVo;
@Controller
@RequestMapping("/tunnelevent")
public class TunnelEventController extends EventBaseController {
@RequestMapping(value = "/reflectEventKey")
@ResponseBody
public String reflectkey(String type) {
try{
System.out.println("reflect");
String property="ttt";
String key=(String) ClassUtil.finalValue(type,property);
return key;
}
catch (Exception e){
System.out.println("读取事件类型失败!");
return "";
}
}
}
没有运行到system.out.println("reflect"),为什么ajax不能调用tunnelevent/reflectEventKey?
如果为整个类定义一个@RequestMapping,然后为每个方法指定一个@RequestMapping,怎样直接写方法的url进行调用 展开
1个回答
展开全部
你用firebug调试下 你写的ajax脚本肯定有问题
简单看了下 data:{type:type},
这一行就是错误的 type怎么可能是type
改一下 在试试吧
简单看了下 data:{type:type},
这一行就是错误的 type怎么可能是type
改一下 在试试吧
追问
data:{type:type}是
function reflecr(){
var type=document.all("typeof").value;
ajax
}
和调用的方法参数public String reflectkey(String type) {}
追答
那你输出看看type是什么值?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询