有员工注册。我已经通过ajax调用将值发送给控制器。控制器已获得值(我在调试中看到了值)。但是当我在jquery中提醒响应时:
success: function(response){
// var obj = JSON.parse(response);
var obj = jQuery.parseJSON(JSON.stringify(response));
alert(response);
},
显示[object:object] 但我想要这种类型的回应,比如:
这是我的JSP页面:
var company = [] ;
var from = [] ;
var to = [] ;
var year =[];
var organization =[];
var topic =[];
var duration =[];
jQuery(document).ready(function($) {
var files;
var exp = $('input:radio[name=exp]:checked').val();
var training = $('input:radio[name=training]:checked').val();
$("#pexp").hide();
$("#fieldexp").hide();
$("#ptrain").hide();
$("#fieldtrain").hide();
$("#btnSubmit").click(function(){
addEmployeeOnlineInformation();
processFileUpload();
});
$("#image").on('change',prepareLoad);
$('input[type=radio][name=exp]').change(function() {
if (this.value == 'Yes') {
$("#pexp").show();
addExp();
}
else if (this.value == 'No') {
$("#pexp").hide();
$("#addmorefieldexp").empty();
}
});
$('input[type=radio][name=train]').change(function() {
if (this.value == 'Yes') {
$("#ptrain").show();
addTrain();
}
else if (this.value == 'No') {
$("#ptrain").hide();
$("#addmorefieldtrain").empty();
}
});
});
function addExp(){
var scntDiv = $('#addmorefieldexp');
$('#pexp').live('click', function() {
$( ' <br><br>'
+'<label for="inputPassword3" class="col-sm-2 control-label">Company Name</label>'
+'<div class="col-sm-2">'
+'<input type="text" class="form-control" name="field_company[]">'
+'</div>'
+'<label for="inputPassword3" class="col-sm-1 control-label">From</label>'
+'<div class="col-sm-2">'
+'<input type="text" class="form-control" name="field_from[]">'
+'</div>'
+'<label for="inputPassword3" class="col-sm-1 control-label">To</label>'
+'<div class="col-sm-2">'
+'<input type="text" class="form-control" name="field_to[]">'
+'</div>'
+'<label for="inputPassword3" class="col-sm-1 control-label">Year</label>'
+'<div class="col-sm-1">'
+'<input type="text" class="form-control" name="field_year[]">'
+'</div>').appendTo(scntDiv);
return false;
});
}
function addTrain(){
var scntDiv = $('#addmorefieldtrain');
$('#ptrain').live('click', function() {
$( '<div class="form-group text-center" id="fieldtrain">'
+'<label for="inputPassword3" class="col-sm-2 control-label">Organization</label>'
+'<div class="col-sm-2">'
+'<input type="text" class="form-control" name="field_organization[]">'
+'</div>'
+'<label for="inputPassword3" class="col-sm-2 control-label">Topic</label>'
+'<div class="col-sm-2">'
+'<input type="text" class="form-control" name="field_topic[]">'
+'</div>'
+'<label for="inputPassword3" class="col-sm-2 control-label">Duration</label>'
+'<div class="col-sm-2">'
+'<input type="text" class="form-control" name="field_duration[]">'
+'</div>'
+'</div>').appendTo(scntDiv);
return false;
});
}
function array(){
var inps = document.getElementsByName('field_company[]');
for (var i = 0; i <inps.length; i++) {
var inp=inps[i];
company.push(inp.value);
}
var inps = document.getElementsByName('field_from[]');
for (var i = 0; i <inps.length; i++) {
var inp=inps[i];
from.push(inp.value);
}
var inps = document.getElementsByName('field_to[]');
for (var i = 0; i <inps.length; i++) {
var inp=inps[i];
to.push(inp.value);
}
var inps = document.getElementsByName('field_year[]');
for (var i = 0; i <inps.length; i++) {
var inp=inps[i];
year.push(inp.value);
}
var inps = document.getElementsByName('field_organization[]');
for (var i = 0; i <inps.length; i++) {
var inp=inps[i];
organization.push(inp.value);
}
var inps = document.getElementsByName('field_topic[]');
for (var i = 0; i <inps.length; i++) {
var inp=inps[i];
topic.push(inp.value);
}
var inps = document.getElementsByName('field_duration[]');
for (var i = 0; i <inps.length; i++) {
var inp=inps[i];
duration.push(inp.value);
}
}
function addEmployeeOnlineInformation(){
array();
var gender = $('#gender').val();
var blood = $('#blood').val();
$.ajax({
type: "post",
url: "http://localhost:8080/EmployeeOnlineRegistrationForm/employee",
cache: false,
data:'name=' + $("#name").val()
+"&fname=" + $("#fname").val()
+"&mname=" + $("#mname").val()
+"&nid=" + $("#nid").val()
+"&age=" + $("#age").val()
+"&blood=" + blood
+"&gender=" + gender
+"&caddress=" + $("#caddress").val()
+"&paddress=" + $("#paddress").val()
+"&paddress=" + $("#paddress").val()
+"&pdegree=" + $("#pdegree").val()
+"&puniversity=" + $("#puniversity").val()
+"&pyear="+ $("#pyear").val()
+"&presult=" + $("#presult").val()
+"&mdegree=" + $("#mdegree").val()
+"&muniversity=" + $("#muniversity").val()
+"&mresult=" + $("#mresult").val()
+"&myear=" + $("#myear").val()
+"&bdegree=" + $("#bdegree").val()
+"&buniversity=" + $("#buniversity").val()
+"&bresult=" + $("#bresult").val()
+"&byear=" + $("#byear").val()
+"&hdegree=" + $("#hdegree").val()
+"&college=" + $("#college").val()
+"&hresult=" + $("#hresult").val()
+"&hyear=" + $("#hyear").val()
+"&sdegree=" + $("#sdegree").val()
+"&school=" + $("#school").val()
+"&sresult=" + $("#sresult").val()
+"&syear=" + $("#syear").val()
+"&date=" + $("#date").val()
+"&department=" + $("#department").val()
+"&location=" + $("#location").val()
+"&company=" + company
+"&from=" + from
+"&to=" + to
+"&year=" + year
+"&organization=" + organization
+"&topic=" + topic
+"&duration=" + duration,
success: function(response){
// var obj = JSON.parse(response);
var obj = jQuery.parseJSON(JSON.stringify(response));
alert(obj);
},
error: function(){
alert('Error while request..');
}
});
}
function prepareLoad(event)
{
files=event.target.files;
}
function processFileUpload()
{
var oMyForm = new FormData();
oMyForm.append("file", files[0]);
$
.ajax({dataType : 'text',
url : "http://localhost:8080/EmployeeOnlineRegistrationForm/uploadMyFile",
data : oMyForm,
type : "POST",
enctype: 'multipart/form-data',
processData: false,
contentType:false,
success : function(result) {
alert(result);
},
error : function(result){
alert('error'+result);
}
});
}
这是我的控制器:
@Controller
public class EmployeeRegistrationController {
@RequestMapping(value="/employee",method = RequestMethod.POST)
public @ResponseBody
EmployeeOnlineRegistrationForm add(HttpServletRequest request, HttpServletResponse response)
throws Exception {
EmployeeOnlineRegistrationForm employeeregistration = new EmployeeOnlineRegistrationForm();
String name = request.getParameter("name");
String fname = request.getParameter("fname");
String mname = request.getParameter("mname");
String nid = request.getParameter("nid");
String age = request.getParameter("age");
String blood = request.getParameter("blood");
String gender = request.getParameter("gender");
String caddress = request.getParameter("caddress");
String paddress = request.getParameter("paddress");
String pdegree = request.getParameter("pdegree");
String puniversity = request.getParameter("puniversity");
String pyear = request.getParameter("pyear");
String presult = request.getParameter("presult");
String mdegree = request.getParameter("mdegree");
String muniversity = request.getParameter("muniversity");
String myear = request.getParameter("myear");
String mresult = request.getParameter("mresult");
String bdegree = request.getParameter("bdegree");
String buniversity = request.getParameter("buniversity");
String byear = request.getParameter("byear");
String bresult = request.getParameter("bresult");
String hdegree = request.getParameter("hdegree");
String college = request.getParameter("college");
String hyear = request.getParameter("hyear");
String hresult = request.getParameter("hresult");
String sdegree = request.getParameter("sdegree");
String school = request.getParameter("school");
String syear = request.getParameter("syear");
String sresult = request.getParameter("sresult");
String date = request.getParameter("date");
String location = request.getParameter("location");
String department =request.getParameter("department");
String[] company =request.getParameter("company").split(",");
String[] from =request.getParameter("from").split(",");
String[] to =request.getParameter("to").split(",");
String[] year =request.getParameter("year").split(",");
String[] organization =request.getParameter("organization").split(",");
String[] topic =request.getParameter("topic").split(",");
String[] duration =request.getParameter("duration").split(",");
employeeregistration.setName(name);
employeeregistration.setFname(fname);
employeeregistration.setMname(mname);
employeeregistration.setNid(nid);
employeeregistration.setAge(age);
employeeregistration.setBlood(blood);
employeeregistration.setGender(gender);
employeeregistration.setCaddress(caddress);
employeeregistration.setPaddress(paddress);
employeeregistration.setPdegree(pdegree);
employeeregistration.setPuniversity(puniversity);
employeeregistration.setPyear(pyear);
employeeregistration.setPresult(presult);
employeeregistration.setMdegree(mdegree);
employeeregistration.setMuniversity(muniversity);
employeeregistration.setMyear(myear);
employeeregistration.setMresult(mresult);
employeeregistration.setBdegree(bdegree);
employeeregistration.setBuniversity(buniversity);
employeeregistration.setByear(byear);
employeeregistration.setBresult(bresult);
employeeregistration.setHdegree(hdegree);
employeeregistration.setCollege(college);
employeeregistration.setHyear(hyear);
employeeregistration.setHresult(hresult);
employeeregistration.setSdegree(sdegree);
employeeregistration.setSchool(school);
employeeregistration.setSyear(syear);
employeeregistration.setSresult(sresult);
employeeregistration.setDate(date);
employeeregistration.setDepartment(department);
employeeregistration.setLocation(location);
employeeregistration.setCompany(company);
employeeregistration.setFrom(from);
employeeregistration.setTo(to);
employeeregistration.setYear(year);
employeeregistration.setOrganization(organization);
employeeregistration.setTopic(topic);
employeeregistration.setDuration(duration);
System.out.println(" name "+name+" fname "+fname+" mname "+mname+" blood "+blood+" gender "+ gender+" caddress "+caddress+" paddress "+paddress
+" pyear "+pyear+" pdegree "+pdegree+" puniversity "+puniversity+" presult "+presult
+" myear "+myear+" mdegree "+mdegree+" muniversity "+muniversity+" mresult "+mresult
+" byear "+byear+" bdegree "+bdegree+" buniversity "+buniversity+" bresult "+bresult
+" hyear "+hyear+" hdegree "+hdegree+" college "+college+" hresult "+hresult
+" syear "+syear+" sdegree "+sdegree+" school "+school+" sresult "+sresult
+" date "+date+" location "+location+" department "+department);
return employeeregistration;
}
@RequestMapping(value = "/welcome",method = RequestMethod.GET)
public String index(ModelMap model){
model.addAttribute("message", "Spring MVC XML Config Example");
return "index";
}
@RequestMapping(value = "/uploadMyFile", method = RequestMethod.POST)
@ResponseBody
public String handleFileUpload(MultipartHttpServletRequest request)
throws Exception {
Iterator<String> itrator = request.getFileNames();
MultipartFile multiFile = request.getFile(itrator.next());
try {
String fileName=multiFile.getOriginalFilename();
//String path=request.getSession().getServletContext().getRealPath("/");
//System.out.println(path);
//making directories for our required path.
byte[] bytes = multiFile.getBytes();
//File directory= new File(path+ "/image");
File directory= new File("/E:/Java_Project/EmployeeOnlineRegistrationForm/src/main/webapp/resources/image");
if(!directory.exists())
directory.mkdirs();
// saving the file
File file=new File(directory.getAbsolutePath()+System.getProperty("file.separator")+multiFile.getOriginalFilename());
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(file));
stream.write(bytes);
stream.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Error while loading the file");
}
return "File Uploaded Successfully";
}
如何从控制器获得此响应?
答案 0 :(得分:0)
您的回复是javascript对象。您无法提醒object.Access对象属性,如下所示:
var obj = JSON.parse(response);
alert(obj.country);
这会提醒马来西亚。就像访问其他财产一样。
检查alert(typeof(response));
输出对象。
JSON.strngfy()
将JavaScript对象转换为字符串。所以你的输出是这样的。在这里更多... http://www.w3schools.com/js/js_json_stringify.asp
答案 1 :(得分:0)
预计使用[object Object]
时获取alert
。 [object Object]
是对象的默认字符串表示形式,alert
将其参数转换为字符串。参见:
var obj = {foo: 42};
alert(obj);
console.log(obj.toString());
&#13;
如果您想调试您的代码,您应该使用console.log
或console.dir
,不 alert
:
var obj = {foo: 42};
console.log(obj);
&#13;
您只需要访问response
上的正确属性即可。有关具体方法,请参阅Access / process (nested) objects, arrays or JSON。