scalatra - No params in route for ajax post -


when post data scalatra route, no params seen.

on client:

$.ajax({     type: 'post',     url: window.location.origin + '/move',     contenttype: 'application/octet-stream; charset=utf-8',     data: {gameid: 1, from: from.touppercase(), to: to.touppercase()},     success: function(result) {         console.log('ok posting move', result);     },     error: function(e) {         console.log('error posting move', e);     } }); 

in dev tools network view:

payload gameid=1&from=b1&to=c3 

in scalatra route:

params("gameid") // -> java.util.nosuchelementexception: key not found: gameid 

however, if change ajax call removing data field , setting url to:

    type: 'post',     url: window.location.origin + '/move?gameid=1&from=' +          from.touppercase() + '&to=' + to.touppercase(), 

then scalatra can see params ok, though seems wrong put params in query string post.

why can't scalatra see params when posting data?

you need use application/x-www-form-urlencoded content type:

script(type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js")  :javascript   $(function() {     $("#btn").click(function() {       $.ajax({         type: 'post',         url: window.location.origin + '/form',         contenttype: 'application/x-www-form-urlencoded; charset=utf-8',         data: {gameid: 1, from: "from", to: "to"}       });     });   });   span#btn hello 

in scalatra application:

post("/form") {   val payload = {     gameid <- params.getas[int]("gameid")     <- params.getas[string]("from")     <- params.getas[string]("to")   } yield (gameid, from, to)    payload }        

for details please take @ servlet specification.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -