在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Java  HTML/ ajax發(fā)送json,為什么后臺(tái)什么也得不到?

ajax發(fā)送json,為什么后臺(tái)什么也得不到?

<html>

<body>

    <input  id="customerId" >
    <input  id ="address" >
    <input     id="123" type="submit" >


</body>
<script src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$("#123").click(function() {
  
    $.ajax({
        type: "POST",
        url: "http://10.2.163.2:7001/chatClientService/",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(GetJsonData()),
        dataType: "json",
        success: function (message) {
            
        },
        error: function (message) {
            
        }
    });
});

function GetJsonData() {
    var json = {
        "customerId": "7800071499",
        "address": "10.2.162.2"
       
    };
    return json;
}
</script>

</html>
@Controller(value = "blackServiceAction")
@Scope("prototype")
public class BlackServiceAction extends ActionSupport {
    private static final long serialVersionUID = -5155883818336614584L;
    @Autowired
    private BlackInfoDaoImpl blackInfoDao;
    public String execute() throws ServletException, IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        InputStream ins = request.getInputStream();
        
        InputStreamEntity entity = new InputStreamEntity(ins, 1024);
        String requestContent = EntityUtils.toString(entity, "utf-8");
        JSONObject requestJson = JSONObject.fromObject(requestContent);
        try {
            
            String customerId = requestJson.getString("customerId");
            String address = requestJson.getString("address");
            String result = isBlack(customerId,address);
            response.getWriter().print(result);
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }
回答
編輯回答
傻叼

HTMLFormElement enctype

根本就沒(méi)有application/json

2017年2月7日 18:37
編輯回答
貓館

你好,F(xiàn)orm 表單并無(wú)此屬性值,你這么寫(xiě)還是會(huì)當(dāng)做 application/x-www-form-urlencoded 來(lái)處理,發(fā)送的是 username=小華&age=18 這樣的格式。

發(fā)送 JSON 格式的可以用 AJAX,設(shè)置 xhr.setRequestHeader('Content-Type', 'application/json');,然后再 send 的時(shí)候就可以這么發(fā)送:

xhr.send(JSON.stringify({
    username,
    password
}));

當(dāng)然你也可以使用 jQuery,就像下面這樣:

$.ajax({
    url:'user.php',
    type:'post',
    data:{
        user: '小華',
        age: 18
    }
});
2018年5月10日 08:17
編輯回答
終相守

給個(gè)默認(rèn)值?

2018年9月19日 15:09