您的位置:首页 > Web前端 > JQuery

公司实习对前端ajax,jquery的认识

2017-07-27 15:35 351 查看
1、前端发送ajax请求之后获取请求的列表

$(document).ready(function() {
$.ajax({
url : '<%=request.getContextPath()%>/background/newskind/newskindList/0',
dataType : 'json',
cache : false,
aysnc : false,
success : function(response) {
$.each(response,function(n,value) {
$('#newskindSelect').append($("<option></option>").attr("value",value.id).text(value.name));

});
}
});
$.ajax({
url : '<%=request.getContextPath()%>/background/specialschool/specialschoolList/0',
dataType : 'json',
cache : false,
aysnc : false,
success : function(response) {

$.each(response,function(n,value) {
$('#SpeciaSchoolSelect').append($("<option></option>").attr("value",value.id).text(value.schoolName));

});
}
});

$('#newskindSelect').on("change",function(){
if(($("#newskindSelect").find("option:selected").text()=='院校专家'))
{
$("#SpeciaSchoolSpan").show();
$("#SpeciaSchoollabel").show();

}else{
$("#SpeciaSchoolSpan").hide();
$("#SpeciaSchoollabel").hide();
}
}) ;

});

<div class="row cl">
<label class="form-label col-2" ><span class="c-red">*</span>新闻类别:</label>
<div class="formControls col-2">
<span class="select-box"> <select id="newskindSelect" name="newskindId"
class="select">
<option value="1">全部分类</option>
</select>
</span>
</div>

<label  id="SpeciaSchoollabel"  class="form-label col-2" style="display:none"><span class="c-red">*</span>院校名称:</label>
<div class="formControls col-2">
<span class="select-box"   id="SpeciaSchoolSpan" style="display:none">
<select name="speciaSchool"   id="SpeciaSchoolSelect"
class="select">
<option value="1">全部分类</option>
</select>
</span>
</div>
2、前端发送ajax请求上传文件http://www.cnblogs.com/BigDreamer/p/5506340.html

$('.sub').click(function(e){

var data1 = new FormData($('#formupload')[0]);

$.ajax({
type: 'post',
url: "http://localhost:8080/pinghua-web/background/partner/create",
data: data1,
contentType: false,
processData: false,
dataType:'text',
success: function(data){
if(data=="success"){
alert("上传成功");
}
},error:function(err){ alert("err"+err);console.log(err);} });});



后端spring mvc

@RequestMapping(value = "/create", method = RequestMethod.POST)
public void create(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="file" ,required=false) MultipartFile file, @Valid  SchoolPartnerDTO schoolpartner, BindingResult result){
LOG.debug("*************** partner create, request str from web :" + schoolpartner + "***************");
String fileName=null;
if(null!=file)
{
fileName=file.getOriginalFilename();
File introFile=new File(request.getServletContext().getRealPath("WEB-INF/intro"),fileName);

try{
file.transferTo(introFile);
}catch(IOException e){
e.printStackTrace();
}
}
String path="WEB-INF/intro/"+fileName;
schoolpartner.setIntro(path);
schoolpartner.setAddtime(new Date());
schoolpartner.setState(0);
String msg="success";

Assert.notNull(schoolpartner);

SchoolPartnerDTO newsTemp = partnerService.addSchoolPartner(schoolpartner);

response.setHeader("Access-Control-Allow-Origin", "*");
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.print(msg);

}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: