简体   繁体   中英

AJAX POST Request returning 404 on Spring MVC

I'm working on a project where I need to submit a JSOn object using AJAX to Spring controller. But I'm getting 404 on submission. Please, can somebody tell me what the problem is:

My AJAX Request :

    $.ajax({
        url: 'NewTestApp/chkDetails/',
        type : 'POST',
        data : 'pwd='+ p,       
        timeout: 15000,
        async : false,
        dataType: 'json',
                success: function (data, textStatus, jqXHR) {
                    displayThings(data);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $('#error').show();
            }
        });
    }
}

My Controller :

@RequestMapping(value = "/chkDetails", method = RequestMethod.POST)
@ResponseBody
public JSONObject getDetails(@RequestParam(value = "pwd")Object sPassword) throws IOException, ParseException
{
    JSONObject obj = (JSONObject) JSONValue.parse(sPassword.toString());
    JSONObject retObj;

    if(obj.isEmpty())
    {
        System.out.println("hihi");
    }       
    retObj = chk.chkStrength(obj);
    return retObj;  
}

My web.xml :

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>

and My spring-servlet.xml :

<context:component-scan
    base-package="main.pwd.controller" />


<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="suffix" value=".jsp" />
    <property name="prefix" value="/WEB-INF/jsp/" />
</bean>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="urlMap">           
        <map>
            <entry key="/checkDetails.html">
                <ref bean="PasswordServiceController"/>
            </entry>
        </map>
    </property>

</bean>
  <bean id="PasswordServiceController" class="main.pwd.controller.PasswordServiceController"> </bean>

I'm very new to spring and this is my first project. Pretty sure I've done something wrong with the mapping.

If directory structure helps :

NewTestApp | WebContent | |-WEB-INF | | | |-web.xml | |-spring-servlet.xml | |-jsp | | | |- checkDetails.jsp |-index.jsp

index.jsp is able to call checkDetails.jsp. Also, there is no problem with AJAX JSON submission, the application is working perfectly when m not implementing it as a Spring MVC.

Do I need to add another mapping for it ?

Your servlet mapping for dispatcher servlet is *.html, so your requests also should be to /NewTestApp/chkDetails.html for it to be handled by your Spring controller. If you want it to be handled by /NewTestApp/chkDetails then the dispatcher servlet mapping should be /

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM