2015-09-20 40 views
1

我使用sails.js v0.11.1通过请求主体蓝图动作模型。 在我routes.js文件我有这样的路线写:绑定Sails.js路线

'POST /user' : { 
    model:  'user', 
    blueprint: 'create' 
}, 

我的身体发送POST请求(通过postman),以/user与此:

{ 
    userName : "firstUser", 
    password : "secretPwd5779", 
    firstName : "William", 
    lastName : "Askey" 
} 

,但我得到这个回应:

{ 
    "error": "E_VALIDATION", 
    "status": 400, 
    "summary": "4 attributes are invalid", 
    "model": "User", 
    "invalidAttributes": { 
     "userName": [ 
      { 
       "rule": "string", 
       "message": "`undefined` should be a string (instead of \"null\", which is a object)" 
      }, 
      { 
       "rule": "required", 
       "message": "\"required\" validation rule failed for input: null" 
      } 
     ], 
     "password": [ 
      { 
       "rule": "string", 
       "message": "`undefined` should be a string (instead of \"null\", which is a object)" 
      }, 
      { 
       "rule": "required", 
       "message": "\"required\" validation rule failed for input: null" 
      } 
     ], 
     "firstName": [ 
      { 
       "rule": "string", 
       "message": "`undefined` should be a string (instead of \"null\", which is a object)" 
      }, 
      { 
       "rule": "required", 
       "message": "\"required\" validation rule failed for input: null" 
      } 
     ], 
     "lastName": [ 
      { 
       "rule": "string", 
       "message": "`undefined` should be a string (instead of \"null\", which is a object)" 
      }, 
      { 
       "rule": "required", 
       "message": "\"required\" validation rule failed for input: null" 
      } 
     ] 
    } 
} 

根据Sails.js documentation我写了正确的路线。

通过URL参数,使这一请求(即发出请求到/user?userName=firstUser&password=secretPwd5779&firstName=William&lastName=Askey作品就好了我如何能得到这个无需通过URL参数发送数据的工作

+0

您能不能告诉我们的页面(表单),JavaScript的,你发布的数据? – num8er

+0

我使用REST客户邮差(https://www.getpostman.com)提出这项要求 – Xecure

回答

2

@ num8er的回答对我来说非常有洞察力(作为新手sailsjs /节点程序员),但是我的问题实际上是由非法pm2实例引起的。

我发出请求被发送到一个流氓实例,其中我写的路由/变化并未在代码中体现。希望这可以帮助那里的人。

2

标准方式:?

这是您的形式:

<form class="ajax" method="post" action="/user"> 
<input name="userName"> 
<input name="password"> 
<input name="firstName"> 
<input name="lastName"> 
<button type="submit">CREATE</button> 
</form> 
<div id="result"></div> 

,这是前端部分的js(需要的jquery):

$(function(){ 
    $('form.ajax').on('submit', function(e){ 
     e.preventDefault(); 
     var method = $(this).attr('method'); 
     var url = $(this).attr('action'); 
     var data = $(this).serializeArray(); 

     $.ajax({ 
     url: url, 
     method: method, 
     data: data, 
     success: function(response) { 
      $('#result').html(JSON.stringify(response)); 
     }, 
     error: function() { 
     alert('Error! Try again later.'); 
     } 
     }); 
    }); 
}); 

但你发送JSON的身体,所以必须启用中间件将JSON身体转换成request.body。
它被称为body-parser。查看屏幕截图:http://joxi.ru/752aJJbhj45DA0

+0

实际上我不是通过表单,而是一个REST客户端 – Xecure

+0

阅读非常非常最后两句 – num8er

+0

嗯发出请求,我已经取消了该行,但仍然没有任何评论。这是我的文件看起来像:https://infinit.io/_/nxNmm9w,我从服务器获取响应:'不能得到/用户/ create' – Xecure