【问题标题】:{ "readyState": 0,"status": 0,"statusText": "NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost:8088/'." }{“readyState”:0,“status”:0,“statusText”:“NetworkError:无法在“XMLHttpRequest”上执行“发送”:无法加载“http://localhost:8088/”。” }
【发布时间】:2018-12-29 22:52:39
【问题描述】:

我正在尝试通过 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>

请求状态:

【问题讨论】:

    标签: java web-services spring-mvc web.xml spring-restcontroller


    【解决方案1】:

    请告诉我请求状态。 Chrome:F12 -> 网络 -> 并在那里选择请求。 看起来像安全问题,请提供您的安全配置,并禁用 cors()。 或者尝试将跨域设置为'false'。

    【讨论】:

    • 请求状态为 200 并且设置 Cross domain : false 也没有帮助。
    • 怎么可能?如果没有什么不返回状态,例如,我建议将是 404 或 400。不过好吧,试试我上面的建议。
    • 你有安全网络配置吗?
    • 不,我没有安全网络配置。所有文件都粘贴在这里,并给出了项目结构。
    • 拜托,看看stackoverflow.com/questions/44697883/… 我有同样的问题,关闭 cors() 和 cref() 对我有帮助。祝你好运)
    猜你喜欢
    • 2016-03-09
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 2013-10-24
    • 2018-12-24
    • 2014-12-17
    • 2011-10-24
    • 2014-09-21
    相关资源
    最近更新 更多