0

JQUERY:

$.ajax({
        datatype:"json",
        url:"<%=request.getContextPath()%>/appendStudentView.page",
    type: 'post',
    success: function(data, status) {
        alert("status=="+data)
        },
    error: function(xhr, desc, err) {
        alert("xhr=="+xhr+"Desc: " + desc + "\nErr:" + err);
        }
    });

SPRING CONTROLLER

/**
 * Handles request for adding two numbers
 */
@RequestMapping(value = "/appendStudentView.page")
public @ResponseBody String appendStudentField() {

    List xx=new ArrayList();
    xx.add("CONTROLLER");
return xx;
}

I am calling the appendStudentField() method through JQUERY AJAX and returning a list .I am not getting the List xx in the response of the AJAX Call.

Kindly help it .

Thx Randy

1
  • Are you getting any ERRORS from either the SERVER or in the JavaScript CONSOLE? Commented May 25, 2011 at 15:42

2 Answers 2

1

Do you have Jackson on your classpath? Spring needs Jackson to output JSON.

This tag registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans that are required for Spring MVC to dispatch requests to @Controllers. The tag configures those two beans with sensible defaults based on what is present in your classpath. The defaults are:
...

  • Support for reading and writing JSON, if Jackson is present on the classpath.

Source: Configuring Spring MVC > 15.12.1. mvc:annotation-driven

Sign up to request clarification or add additional context in comments.

Comments

0

Couldn't you just use a Model and pass your variables that way? Here is an example bit of code I use.

@Controller
@Scope("prototype")
@RequestMapping("/favorites")
public class FavoritesController {

    protected final Log logger = LogFactory.getLog(getClass());

    @Autowired
    FavoriteService favoriteService;

        @RequestMapping(method = RequestMethod.POST)
        public void handle(String circuit, String customer, String action, Model model) {

        String userid = SecurityContextHolder.getContext().getAuthentication().getName();

        List<Map<String, String>> favorites = null;

        if (action.equals("save")) {
            favoriteService.setFavorite(userid, circuit, customer);
            favorites = favoriteService.getFavorites(userid);
        }

        if (action.equals("delete")) {
            favoriteService.deleteFavorite(userid, circuit);
            favorites = favoriteService.getFavorites(userid);
        }

        model.addAttribute("userid", userid);
        model.addAttribute("circuit", circuit);
        model.addAttribute("customer", customer);
        model.addAttribute("favorites", favorites);
    }
}

[Edited to add the jquery portion of this]

   // **************************************** 
//  SAVE TO FAVORITES 
// ****************************************
$("#save-to-favorite").live("click", function() {
    var saveCircuit = $(this).attr('circuit');
    var saveCustomer = $(this).attr('customer');

    var data = "action=save&circuit=" + saveCircuit + "&customer=" + saveCustomer;
    $.ajax( {
        type : "POST",
        url : "favorites.html",
        data : data,
        success : function(xhr) {
            $("#favorite-list").html(xhr);
        },
        error : function(xhr) {
            var response = xhr.responseText;
            response = response.replace(/<html>.+<body>/i, "")
            response = response.replace(/<\/body><\/html>/i, "")

            alert(response);
        }
    });
});

// ****************************************
// DELETE FROM FAVORITES
// ****************************************
$(".delete-favorite-icon").live("click", function() {
    var deleteCircuit = $(this).attr('circuit');

    var data = "action=delete&circuit=" + deleteCircuit;
    $.ajax( {
        type : "POST",
        url : "favorites.html",
        data : data,
        success : function(xhr) {
            $("#favorite-list").html(xhr);
        },
        error : function(xhr) {
            var response = xhr.responseText;
            response = response.replace(/<html>.+<body>/i, "")
            response = response.replace(/<\/body><\/html>/i, "")

            alert(response);
        }
    });
});

`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.