Get your CodeRanch badge!*
The moose likes Spring and the fly likes How to call a Spring controller method from a jquery ajax call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to call a Spring controller method from a jquery ajax call" Watch "How to call a Spring controller method from a jquery ajax call" New topic
Author

How to call a Spring controller method from a jquery ajax call

silamala ramesh babu
Greenhorn

Joined: Dec 08, 2011
Posts: 20

Hi iam using Jquery to call controller method My code look like this in jsp.

function sendAppRequest()
{
var friendSelector = j$("#jfmfs-container").data('jfmfs');
var selectedFriends = friendSelector.getSelectedIds();
var str = selectedFriends.join(",");

j$.ajax({

type: 'POST',
url: '/invitefriends/checkRequest.json',
data: {toProviderIds: str},
dataType: 'text',
success: function() {
return true;
//console.log("### Data = " + data);
}
});




FB.ui({method: 'apprequests',
message: 'Please have a look at Reward TV app',
to: selectedFriends.join(","),
}, requestCallback);

return false;
}



And i my controller another method is getting called by the Jquery ajax so i used the same how they are calling and i created a new method in the same controller like this



@RequestMapping(value="/invitefriends/updateFBRequest.json", method=RequestMethod.POST)
public void updateFBRequest(
HttpServletRequest request,
@RequestParam(value="appRequestId", required = true) String appRequestId,
@RequestParam(value="toProviderIds", required = true) String toProviderIds)
{
RTVMember member = RequestUtils.getMember(request);
StringTokenizer tokenizer = new StringTokenizer(toProviderIds, ",");
List<String> toProviderIdsList = new ArrayList<String>();
while(tokenizer.hasMoreTokens())
{
toProviderIdsList.add(tokenizer.nextToken());
}

inviteFriendsService.insertInvitedFriendsRequest(member.getMemberID(), appRequestId, toProviderIdsList);
}



@RequestMapping(value="/invitefriends/checkRequest.json", method=RequestMethod.POST)
public void checkRequest(
HttpServletRequest request,
@RequestParam(value="toProviderIds", required = true) String toProviderIds)
{
RTVMember member = RequestUtils.getMember(request);


inviteFriendsService.getAlreadyInvitedFriendsList(member.getMemberID(), toProviderIds);
}



The method in blue colour is getting called correctly from a ajax call but the code in brown colour is not getting called .Both methods are in the same controller.


rsilamala
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Please use Please Use Code Tags I can't really even bring myself to read what you have posted.

What are you doing differently from one Ajax post to the other? What are you logging in your console?


[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to call a Spring controller method from a jquery ajax call
 
Similar Threads
Pass JSON object from server to jQuery and loop on the object
Ajax Request - Call different method on Spring controller
Setting radio button value dynamically and rendering partially after ajax request
Spring model attribute is not populating values
org.hibernate.LazyInitializationException