我试图通过ajax请求访问 PaymentController.java ,这给了我所附屏幕快照中显示的错误。我已经尝试了很多,但无法击中控制器。我的座右铭是击中控制器并获得返回的字符串。困扰我的错误是:作为Spring-MVC的新手,我无法理解此错误的真正原因。
无法加载http://localhost:8088/paymentgateway/payment/2100002:
{
"readyState": 0,
"status": 0,
"statusText": "NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost:8088/paymentgateway/payment/2100002'."
}
我的项目结构如下:
MyAjaxRequest如下:
<script type="text/javascript">
function test(){
var testData = {}
// var testData = {"orderID":"2100002"}
var URL= "http://localhost:8088/paymentgateway/payment/2100002"
$.ajax({
url : URL,
async :false,
type : 'POST',
dataType : 'json',
data: JSON.stringify(testData),
contentType : 'application/json',
mimeType : 'application/json',
crossDomain : true,
success : function(data) {
console.log(JSON.stringify(data,null,4));
alert(data);
},
error : function(data, status, er) {
console.log(JSON.stringify(data,null,4));
console.log("Errors : ");
}
});
}
</script>
PaymentController.java
@Controller
@RequestMapping(value = "/paymentgateway")
public class PaymentController {
@RequestMapping(value = "/payment/{orderID}", method = RequestMethod.POST)
public String paymentGateway(Model model, @PathVariable String orderID)
throws Exception {
System.out.println("inside controller");
String str="Payment Done";
return "Hello";
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>HelloPayment</display-name>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appServlet-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appServlet-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
appServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<context:component-scan base-package="com.majesco.*" />
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
请求状态:
答案 0 :(得分:1)
请向我显示请求状态。 Chrome:F12->网络->,然后在其中选择请求。 看来是安全问题,请提供您的安全配置,并禁用cors()。 或尝试将跨网域设置为“ false”。