Results 1 to 4 of 4

Thread: $.getJSON() problem

Hybrid View

  1. #1

    Default $.getJSON() problem

    I am creating a spring mvc3.0 application.... i have the jquery-1.3.2.js added to the project.... I have the following javascript function which i call on the submit button click....

    function checkUser() { var uname=$('#username').val(); var pword=$('#password').val(); alert(uname); alert(pword);

    $.getJSON("login.html",{username: uname, password: pword},
    function(message){
    alert(message);
    });
    }

    i am using spring mvc 3.0..... Following is the controller that i use...

    package com.web.controller;

    import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMap ping; import org.springframework.web.bind.annotation.RequestMet hod; import org.springframework.web.bind.annotation.RequestPar am; import org.springframework.web.bind.annotation.ResponseBo dy;

    @Controller
    public class LoginController
    {
    @RequestMapping("/login.html")
    public @ResponseBody String getLoginStatus(@RequestParam("username") String username, @RequestParam("password") String password)
    {
    System.out.println("\n\nin login controller\n\n");
    if(username=="apoorvabade" & password=="apoorva123")
    {
    return "Login successful!!!";
    }
    else
    {
    return "Login failed";
    }
    }
    }

    when i click on the submit button, the function corresponding to the /login.html action is not invoked.... i am using the DispatcherServlet to map the requests to the following....

    here is the spring-servlet.xml


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...ring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:component-scan base-package="com.web.controller" />

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

    </beans>


    web.xml:


    <!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd" >

    <web-app>
    <display-name>Spring Ajax Tutorial Project</display-name>
    <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>

    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>


    Can anybody tell me the problem please?

  2. #2
    Join Date
    Aug 2010
    Posts
    5

    Default

    Several applications can be created with the spring mvc 3.0 as the platform. The Jquery should be added to the application. The submit button has an associated function to it. Package com.web.controller is the controller used in the project. The submit button is not working properly. It is not able to invoke the login function associated with it, when it is clicked. The mapping is done using the DispatcherServlet. The code is also given for the verification.

  3. #3
    Join Date
    Nov 2009
    Location
    Montreal, Quebec
    Posts
    398

    Default

    Have you tried using an absolute path in your getJSON request? Does the page making the getJSON request reside in the same relative directory as your app where login.html will map to?

  4. #4
    Join Date
    Sep 2010
    Location
    Medina, OH
    Posts
    4

    Smile Are you crossing domains?

    Are you testing this functionality across domains? Remember for AJAX requests cross-domain calls are a bugger, you'll have to use jsonp request with jquery instead of a plain getJSON which just does a regular JSON ajax call over GET...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •