Jquery 为什么ajax需要一个";。html";我的url中的后缀?
事实上。我被我的Jsp对SpringMVC控制器的第一个ajax调用所困扰。我从互联网上得到这个例子 Ajax javascript的功能如下:Jquery 为什么ajax需要一个";。html";我的url中的后缀?,jquery,html,url,spring-mvc,Jquery,Html,Url,Spring Mvc,事实上。我被我的Jsp对SpringMVC控制器的第一个ajax调用所困扰。我从互联网上得到这个例子 Ajax javascript的功能如下: <script type="text/javascript" src="scripts/jquery.js"></script> <script type="text/javascript"> function doAjax() { $.ajax({ url: 'time.html',
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript">
function doAjax() {
$.ajax({
url: 'time.html',
data: ({name : "me"}),
success: function(data) {
$('#time').html(data);
}
});
}
</script>
从MVC控制器中,您可以看到请求映射为“/time”。通常,如果我们从JSP调用控制器,我们只需要调用/time来访问控制器,但我不明白,为什么我必须从Ajax url:function调用'time.html'?有什么不同?为什么
谁能给我解释一下吗 互联网中的“扩展”部分只是为了方便。事实上,无论是
.html
还是.foobar
还是.cidy.long
都无关紧要,因为问题不在于ajax不接受它。这是您的服务器端脚本所期望的(甚至是按照惯例。您可以将服务器/脚本设置为接受任何您想要的内容),谢谢您的回复。太好了。我对ajax开发还是个新手。我正在寻找一个好的ajax文件上传功能没有表单提交。我的意思是,直接从JSP提交一个文件到我的SpringMVC监听器,我尝试了一些从互联网上找到的方法,似乎没有人真正适合我。请显示你的web.xml。关于上传:看一下(但这是非常主观的,因此不太适合SO。请参阅)。
<button id="demo" onclick="doAjax()" title="Button">Get the time!</button>
<div id="time">
</div>
package com.maxheapsize.springmvc3.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
return new ModelAndView("hello", "message", "Spring MVC Demo");
}
@RequestMapping(value = "/time", method = RequestMethod.GET)
public @ResponseBody String getTime(@RequestParam String name) {
String result = "Time for " + name + " is " + new Date().toString();
return result;
}
}