Angularjs形成json提交

时间:2014-08-26 20:32:11

标签: javascript json angularjs

尝试调用仅需要JSON post请求的github。

尝试通过JSON提交我的angularjs应用程序表单。

到目前为止,我有这个:

<form ng-submit="postComment(issue.issue.number)" enctype='application/json'>

    <textarea name="body" id="" cols="30" rows="5" ng-model="$parent.issueComment" class="form-control"></textarea>
    <input type="submit" class="btn btn-default" id="submit" value="Submit">
</form>

控制器:

$scope.postComment = (issueNumber) ->

            repo.postComment(issueNumber, $scope.issueComment).then (issue) ->
                console.log issue

服务:

postComment: (issueNumber, body) ->
            console.log body
            # body = JSON.stringify({body: body})
            console.log body
            $rootScope.githubToken.post("/repos/#{@owner}/#{@name}/issues/#{issueNumber}/comments", {
                data: {
                    body: body,
                    hi: "hello"
                },
                dataType: "json"
            }).done (response) ->
                console.log response

我一直收到错误:

message: "Problems parsing JSON"

固定

postComment: (issueNumber, body) ->

            $rootScope.githubToken.post("/repos/#{@owner}/#{@name}/issues/#{issueNumber}/comments", {
                data: JSON.stringify({
                    "body": "#{body}"
                }),
                dataType: "json",
                contentType:'application/json'
            }).done (response) ->
                console.log response

1 个答案:

答案 0 :(得分:0)

而不是像

这样的帖子
 .post("/repos/#{@owner}/#{@name}/issues/#{issueNumber}/comments", {
     data: {
         body: body,
         hi: "hello"
     },
     dataType: "json"
  })

尝试

.post("/repos/#{@owner}/#{@name}/issues/#{issueNumber}/comments", {     
      body: body,
      hi: "hello"  
 })

EDIT --------------- 并尝试对你的对象进行字符串化?

var obj = {     
      body: body,
      hi: "hello"  
 };
 obj = stringify(obj);
.post("/repos/#{@owner}/#{@name}/issues/#{issueNumber}/comments", obj)