简体   繁体   中英

SailsJS pending ajax post (NodeJS)

I want to process AJAX data in my sailsJS app but the ajax post data always stop at pending state here is my code at my view.

<script type="text/javascript">
    $('#submit').click(function(){
     $.ajax({
           url: 'users/create',
            type: 'POST',
            data: JSON.stringify($('#username').val()),
            contentType: 'application/json',
            dataType: 'json',
            cache: false, 
            success: function(data){
               alert('Success!');
             }
             ,error: function(jqXHR, textStatus, err){
                  alert('text status '+textStatus+', err '+err);
             } 
          });
     });
 </script>

and here is the code at my controller

var UsersController = {
  create: function (req, res) {
     console.log(req);
 }
 };
 module.exports = UsersController;`

do i missed something? i also set the policies to true all over my controller

Your request is "pending" because you're not sending a response from the server. Your create method needs to end with res.send() or res.json() .

It's not clear what your intention is here--you are overriding the default "create" blueprint for Sails by adding a UserController.create method of your own, but you're not actually creating a User (maybe you haven't gotten to that part yet). If all you want to do is have a POST route that creates a user, you can remove this method entirely and just POST to /user to use the default blueprint create method .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM