Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
silamala ramesh babu
Greenhorn
Posts: 25
Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!