无法进入$.get(seckill.URL.now(),{}, function(result){。。。},报404错误

来源:5-4 秒杀交互

Leon_Ma

2017-05-09 22:24

浏览器可以正常请求到json数据,但是$.get('/seckill/time/now',{},function(result){...}无法获得json数据,报404错误

浏览器http请求头和响应头:

Accept
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8    
Accept-Encoding    gzip, deflate    
Accept-Language    zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3    
Cache-Control    max-age=0    
Connection    keep-alive    
Cookie    JSESSIONID=06FAA701426C4EC382C2E7F11F88779C    
DNT    1    
Host    localhost:8083    
Upgrade-Insecure-Requests    1    
User-Agent    Mozilla/5.0 (Windows NT 6.2; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0

Content-Typeapplication/json;charset=UTF-8    
Date    Tue, 09 May 2017 13:40:32 GMT    
Server    Apache-Coyote/1.1    
Transfer-Encoding    chunked    

$.get()的请求头和响应头:

GET  http://localhost:8083/seckill/time/now

Host: localhost:8083
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0
Accept: */*
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest      //网上查说这里不一样,但不造怎么处理
Referer: http://localhost:8083/seckill-alpha/seckill/1009/detail
Cookie: killPhone=13812345678
DNT: 1
Connection: keep-alive


Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 983
Date: Tue, 09 May 2017 13:41:05 GMT

Controller:

    @RequestMapping(value = "/time/now", method = {RequestMethod.GET,RequestMethod.POST},
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public  SeckillResult<Long> time(){
        Date now = new Date(System.currentTimeMillis());
        logger.info("/time/now=====  ", now);
        System.out.println("/time/now=====  "+now);
        SeckillResult<Long> result = new SeckillResult<Long>(true, now.getTime());
        return result;      
    }

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">
<display-name>secikill-webapp</display-name>
<!--修改servlet版本为3.1-->
<!--配置DispatcherServlet-->
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-*.xml</param-value>
</context-param>
<servlet>
    <servlet-name>servlet-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--配置springmvc需要加载的配置文件
        spring-dao.xml,spring-service.xml, spring-web.xml
        shunxumybatis->spring->springmvc
    -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:*spring/spring-*.xml</param-value>
    </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>servlet-dispatcher</servlet-name>
    <!--默认匹配所有请求,注意静态资源-->
    <url-pattern>/</url-pattern>
</servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 配置SESSION超时,单位是分钟 -->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
  <!--   编码过滤器 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
<!--     <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> -->
    <!-- Spring监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 防止Spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    
</web-app>


写回答 关注

3回答

  • 75克酸奶
    2017-05-20 22:22:35
    已采纳

    $.get('/seckill/time/now',{},function(result)

    ==》$.get('/seckill-alpha/seckill/time/now',{},function(result);

    把项目名加上去试试..



    慕设计165...

    你这里的项目名指代不清楚,搞得我弄了好久,但是还是解决了。详细的说法应该是:$.get('/seckill-alpha/seckill/time/now',{},function(result);这里的seckill-alpha应该指的是你的tomcat部署时的路径,老师里的tomcat服务器部署的应该默认是空的,所以没有问题,但是一般来说我们都会自己选择部署的路径。

    2019-04-15 23:38:35

    共 1 条回复 >

  • 慕设计1656446
    2019-04-16 00:05:25

    或者这样写也可以的:

    URL: {
        now: function () {
            return '/shopping/seckill/time/now';
        },
        exposer: function (seckillId) {
            return '/shopping/seckill/' + seckillId + '/exposer';
        },
        execution: function (seckillId, md5) {
            return '/shopping/seckill/' + seckillId + '/' + md5 + '/execution';
        }
    },

    我们把URL里的返回路径统一设置一下就可以啦。同理,这里的/shopping是我的tomcat服务器的部署路径。

  • 慕设计1656446
    2019-04-15 23:44:26
    改成这样就好啦:
    这是我的修改后的代码,这里的/shopping指的是tomcat的部署路径。
    $.get('/shopping/seckill/time/now', {}, function (result)

Java高并发秒杀API之web层

Java实现高并发秒杀API的第三门课,介绍Web层的设计和实现

66079 学习 · 395 问题

查看课程

相似问题