我准备做一个解析url的程序,结果ajax无法传递数据,这是出了什么问题。用的是SpringBoot+Html+Ajax。
这是index265.html的前端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div>解析</div>
<form th:action="@{/index265}" th:object="${result}" method="post">
<div>输入</div>
<textarea id="kouling" name="kouling" style="width:98%;height:20%"></textarea>
<div>结果</div>
<textarea id="result" name="result" style="width:98%;height:50%"></textarea>
<div>
<input type="button" id="log" value="显示结果"></input>
</div>
</form>
<script th:inline="javascript">
var s='{"tkl":+"'+$("#result").text()+'"}';
var res=encodeURI(s);
$.ajax({
type: "get",
url: "./index265",
async:true,
error:'重新请求',
contentType: "application/json; charset= utf-8",
data: res,
dataType: "json",
success:function(res){
alert(res);
},
error:function (res) {
}
});
$("#log").click(function () {
var a=[[${jsonal}]];
$("#result").text(a);
})
</script>
</body>
</html>
这是后端代码
@RequestMapping("/index265")
@CrossOrigin
@ResponseBody
public Model explain(Model mp){
HashMap<String,String> map=new HashMap<String, String>();
String api="http://api.tbk.dingdanxia.com/tkl/query";
String apikey="mJCrdmqNiY+kaaugbN2de4typJ2M3o6Idqpwq82nxp4=";
String uri = "https://www.dingdanxia.com/index/index/dotestapi.html";
String tkl=map.get("tkl");
String gen="";
String jsonal="";
System.out.println(tkl);
//String a=UserTrain.testPostRequest(api,apikey,tkl,gen);
//System.out.println(a);
//String b=MyJsoup.soup(a);
//System.out.println("乌拉"+b);
if(tkl!=null){
String asjb=MyJsoup.soup(UserTrain.post(uri,api,apikey,tkl,gen));
JSONObject job = JSON.parseObject(asjb);
String url = JSONObject.parseObject(job.getString("data")).getString("url");
String a = url.substring(url.indexOf("account_id"), url.indexOf("app_key"));
String b = url.substring(url.indexOf("feed_id"), url.indexOf("os"));
String c = url.substring(url.indexOf("mediaplatform/") + 14, url.indexOf("?auth_key"));
//System.out.println(a+"#"+b);
String d = a.substring(13, a.length() - 3);
String e = b.substring(10, a.length() - 4);
System.out.println(d + "#" + e + "#" + c);
String upid = d;
String zbid = e;
String topic = c;
jsonal= "{\"upid\":" + upid + "\",\"zbid\":" + zbid + "\",\"topic\":" + topic + "\"}";
}
mp.addAttribute("jsonal",jsonal);
System.out.println(jsonal);
return mp;
}
这是网页的效果图
结果我输入了要解析的文字,输出的却永远是null(也就是前端输入的文字,到后端却显示null)。结果栏也没有要解析的结果。估计大概率是ajax没把数据传到后端的SpringBoot里面。这是为什么呢?怎么解决?