Ajax success issue with React and Rails -


i've got form trying handle react. ajax method in handlesubmit function:

$.ajax({   data: formdata,   datatype: "json",   url: '/requests',   type: "post",   success: function(data, status, xhr) {     console.log('show here on response');     this.setstate({       formsubmitted: true     });   } }); 

when click on submit, can see post request go through , server responds 200. success callback never entered, don't see console message.

my rails controller's action simple:

return render nothing: true, status: 200 if request.xhr? 

the log server response includes:

completed 200 ok in 11ms (views: 2.5ms | activerecord: 2.6ms) 

however when move stuff success callback in ajax request complete callback see error in console:

uncaught typeerror: this.setstate not function 

you seeing error, because this behaves differently inside $.ajax()'s success callback, because jquery uses internally.

suppose executing following ajax call in response click function, then:

var $this = $(this); $.ajax({   data: formdata,   datatype: "json",   url: '/requests',   type: "post",   success: function(data, status, xhr) {     console.log('show here on response');       $this.setstate({       formsubmitted: true     });   } }); 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -