Results 1 to 10 of 17

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

    Default

    I tried the mvc-ajax example in the springsource samples... Even that is not working.... required alerts, messages are not displayed.... Do i need to add anything pther than the normal jquery.js file to unable the jquery ajax calls? PLease help... Its urgent!!!

  3. #3
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    Hi,

    I think you need better understanding of Spring webmvc's mechanics. The way you configured mvc, this is what happens: your json request maps correctly to the getLoginStatus method, but at the end of that method you either return "Login successful" or "Login failed" and thus at the end of method execution your controller tries to navigate to either /WEB-INF/jsp/Login%20successful.jsp or /WEB-INF/jsp/Login%20failed.jsp and I don't think either of them exists. To make this work the way you intend to, you must use a custom view definition which is based upon org.springframework.web.servlet.view.XmlViewResolv er and transforms your Model into a json string, and configure this view resolver in your xml with a priority higher than your InternalResourceViewResolver (see the section about chaining resolver in the Spring reference).
    This, by the way, is exactly what Spring Json does, so please at least consider using that to help you out (I can see you have chosen not to use DWR, which I suggested as the simplest solution for ajax).

  4. #4

    Default

    Actually Spring MVC3.0 is internally using the JacksonJson library.... so i need not require to use any other external library... that is what i have figured out out of http://blog.springsource.com/2010/01...in-spring-3-0/.... And yes.. I understand what you are saying/... I am very new to this concept in all.. Both spring mvc and jquery...

  5. #5
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    Ok, I wasn't aware of this new feature. I have checked the example on the site you have given and it works perfectly for me, did you try to deploy that as it is, without changing anything?

    As I understand , the new json integration will only work if you meet this 2 requirements:

    1) You use the <mvc:annotation-driven/> directive in your webmvc config AND
    2) Jackson is on your classpath.

    By looking at your spring-servlet.xml (please next time use the CODE tag to post code) I see not only that you are not using the <mvc:annotation-driven/>, but also that you are using old 2.5 schema and you are missing the mvc xsd at the beginning of your file.
    So basically, you are trying to use a new, advanced feature available only with latest Spring release but you are using old configuration (and maybe also old Spring, did you update to latest release?). You understand that this could never work; I suggest you install the example in the site and study it more before you try to code your own Spring - Json example.

  6. #6

    Default

    Yes.. I did try the mvc-ajax example... It doesnt give any error... But unfortunately it is not showing any behaviour as expected... eg: If the name exists while creating a new account, it gives "not available" with available list of names.. And that too when tab out of the name field.... It is giving some error if try to create a new account... I dint do any changes here...
    Rest points that you said, I will definitely look into them.... ANd will try to study the example more in details...

  7. #7
    Join Date
    Aug 2010
    Posts
    1

    Default i had a problem in Json

    I'm trying to use json with YUI in spring project and i have the following error :

    No mapping found for HTTP request with URI [/pashajobs-web/landmarks/64/17.json] and i'm using this call "YAHOO.util.Connect.asyncRequest('GET', 'landmarks/' + countryId + '/' + cityId + '.json', callback);"

    in my conroller the mapping as the following:
    @RequestMapping("/landmarks/{countryId}/{cityId}")
    thanks in advance.

Posting Permissions

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