Passing AJAX POST parameters to the Grails controller

I create a social network with Grails and get stuck on giving users the internal page editing file the ability to paste the youtube-url URL into the text field and by pressing the JS button it gives the id from the inserted URL, the ajax message starts with updating the div with a preview image of youtube video

html looks like this:

<g:textField name="videoinput" class="videoinput reLef" value="" /> <span class="daten_videouploadbtn reLef" ></span> <g:render template="/forms/storedVideos" /> 

JS looks like this:

  $('.daten_videouploadbtn').click(function() { var string = document.editProfileForm.videoinput.value; var neu = string.replace(/http[s]?:\/\/(?:[^\.]+\.)*(?:youtube\.com\/(?:v\/|watch\?(?:.*?\&)?v=|embed\/)|youtu.be\/)([\w\-\_]+)/i, '$1'); var id = RegExp.$1; jQuery.ajax({ type:'POST', data:RegExp.$1, url:'${createLink(action: 'addVideo')}', success:function(data,textStatus){jQuery('#storedvideos').html(data);}, error:function(XMLHttpRequest,textStatus,errorThrown){} }); }); 

the controller is as follows:

 def addVideo() { def videoitems = !!%%-- HOW TO PARSE YOUTUBE-ID HERE -%%!! render(template:"/forms/storedVideos", model: [newVideo:videoitems]) } 

and saved videos look:

  <div id="storedvideos"><span><img src="http://img.youtube.com/vi/${newVideo}/default.jpg" width="225px" height="130px"/></span></div> 

I just don’t understand how to catch Ajax Post data and update div with preview image with id inside,

Can someone tell me? this is killing me

+4
source share
2 answers

You must send the data as follows:

 jQuery.ajax({ type: 'POST', data: { value: RegExp.$1 }, ... 

After that, you can access the published data inside your grails controller using params.value .

+4
source

I worked on Grails 2.0.4:

Javascript / ajax

 var data = {requestId: 12456, node: "node1", host: "mynode.com"}; $.ajax({ url: '/myurl', data: JSON.stringify(data), type: 'post', contentType: 'application/json; charset=utf-8', dataType: 'json', success: function() ... }, error: function() ... } }); 

In Grails ....

 def service(){ def jsonObj = request.JSON } 

I like this approach because request.JSON parses the data and returns a ready-to-use object.

+1
source

All Articles