weixin_33725515 2016-05-20 06:35 采纳率: 0%
浏览 18

在Servant中使用Ajax

I use Haskell with servant-0.7.1 fo realisation server.Below is my source code:

type UserRestAPI 
    = "rest" :> "users" :> Get '[JSON] [User]
    :<|> "rest" :> "user" :> ReqBody '[JSON] User :> Post '[PlainText] Text


serverUserRestAPI :: ServerT UserRestAPI AppM
serverUserRestAPI = usersGet :<|> userPost

userPost :: User -> AppM Text
userPost user = do
    newUser <- runDb $ do insert user
    liftIO $ putStrLn $ show newUser
    return $ append (toPathPiece newUser) "
"

The model of User:

let mongoSettings = (mkPersistSettings (ConT ''MongoContext)) {mpsGeneric = False}
    in share [mkPersist mongoSettings, mkMigrate "migrateAll"] [persistLowerCase|
User
    fam   String
    im    String 
    ot    String 
    email String 
    login String
    pswd  String
    deriving Show
|]

$(deriveJSON defaultOptions ''User)

For testin curl was used, as shown below.

curl --verbose --request POST --header "Content-Type: application/json" \
    --data '{"userFam": "Fam", "userIm": "Im", "userOt": "Ot", "userEmail": "mail@mail.ru", "userLogin": "test", "userPswd": "test"}' \
    http://127.0.0.1:3000/rest/user

Everything is working. The data added to the database.But when I use ajax from backend, as shown below.

var formElement = $("#id_form");
var formData = formElement.serializeArray();

var objectData = {};

for(var i = 0; i < formData.length; i++)
    objectData[formData[i].name] = formData[i].value;

$.ajax({
    type: "POST",
    async: true,
    url: "/rest/user",
    dataType: "text",
    cache : false,
    contentType : "application/json",
    data: objectData,
    success: function(result){
        consoloe.log(result)
    },
    error: function(jqXHR, status, err) {
        console.log(err)
    }
});

I get an error!

jquery.js:4 POST http://127.0.0.1:3000/rest/user 400 (Bad Request)

The debugger checked object objectData. All right (Object {userFam: "qqq", userIm: "www", userOt: "eee", userEmail: "rrr", userLogin: "ttt"…}). I can not understand what was going on.

  • 写回答

1条回答 默认 最新

  • weixin_33736048 2016-05-20 07:59
    关注

    it's really a problem with your ajax call as you don't provide a valid JSON object but just objectData.toString() as data: - the common solution is to use JSON.stringify:

    $.ajax({
        type: "POST",
        async: true,
        url: "/rest/user",
        dataType: "json",
        cache : false,
        data: JSON.stringify(objectData),
        success: function(result){
             // ...
        },
        error: function(jqXHR, status, err) {
            console.log(err)
        }
    });
    

    another great example what is wrong with untyped languages ;)

    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示