Application of Ajax (I wrote my own experience, I will add it slowly)

<script type="text/javascript" src="/ddssm/javascipt/jquery-3.3.1.js"></script>

<script type="text/javascript" src="/ddssm/javascipt/jquery-3.3.1.min.js"></script>

The minimum js to be imported.

I am using the SpringMVC+Spring+Mybatis framework here

There are also several required jar packages: json-lib, commons-io, commons-fileupload these jar packages are necessary, and my jar packages are sent to everyone. Add what is missing.


You can use it directly to see if there is any problem, and then check it slowly. . .

First solve the problem of Chinese garbled characters. web.xml

https://blog.csdn.net/niceLiuSir/article/details/78445630?locationNum=5&fps=1 I found it here

<!-- 统一字符编码 -->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
    <init-param>  
        <param-name>forceEncoding</param-name>  
        <param-value>true</param-value>  
    </init-param>  
  </filter>


  <filter-mapping>  
     <filter-name>CharacterEncodingFilter</filter-name>  
     <url-pattern>/*</url-pattern>  

   </filter-mapping> 


Ajax part in jsp

$(function() {
$.ajax({
type : "post",//This is the get/post
url : "/ddssm/studentcontroller/open",//The address to go to
data : { //This is the address that needs to be passed The output value
stid : $("#stid").val(),
stname : $("#stname").val()
},
success : function(msg) {
var list = eval(msg);//put Enter list
var opi = "<option>--Please select--</option>";
var opn = "<option>--Please select--</option>";
$.each(list, function(index, entity) {//traverse list
opi += "<option>" + entity.stid + "</option>";
opn += "<option>" + entity.stname + "</option>";
});
$(" #stid").html(opi); //Put it where needed
$("#stname").html(opn);
}
});
})


Then the body part below the jsp

<form action="/ddssm/studentcontroller/insertOne" method="post">
<table>
<tr>
<td>stid:<select id="stid"></select></td>
</tr>
<tr>
<td>stname:<select id="stname"></select></td>
</tr>
</table>
</form>


Then jump to the Controller layer part

@Controller

@RequestMapping("/studentcontroller")

public class StudentController {
@Autowired
private LoginServices loginservices; @RequestMapping("/open") //student class receives the values ​​of stid and stname public void open(HttpServletResponse response,Student student) throws IOException { List<Student> list = studentservices.selectAll ();//Here is the full check jsonarray.json(response, list);//Check out and return ajax }






}

Here is the jsonarray I wrote myself

public class jsonarray {
public static void json(HttpServletResponse response,Object obj) throws IOException{
JSONArray json = JSONArray.fromObject(obj);
PrintWriter writer = response.getWriter();
writer.write(json.toString());
writer.close();
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325654455&siteId=291194637