【问题标题】:How to POST this Output to the API如何将此输出发布到 API
【发布时间】:2015-12-06 08:04:15
【问题描述】:

您好,我创建了一个从表单获取数据的 JSON 对象,现在我想将其发布到 redmine API。这就是我到目前为止所做的。

<script>
// This is the creation of JSON object 
$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return {"project":o};
};

// This is the API linking and POSTING

    $(document).ready(function(){
        $("#submit").on('click', function(){
            // send ajax
            $.ajax({
                url: 'http://localhost/redmine/projects.json', // url where to submit the request
                type : "post", // type of action POST || GET
                dataType : 'jsonp',
                headers: { 'X-Redmine-API-Key': 'admin' }, 
                data : JSON.stringify($('form').serializeObject()), // post data || get data
                success : function(result) {
                    // you can see the result from the console
                    // tab of the developer tools
                    alert("Sucess");
                    console.log(result);
                },
                error: function(xhr, resp, text) {
                    console.log(xhr, resp, text);
                }
            })
        });
    });
</script>
<form action="" method="post">
First Name:<input type="text" name="name" maxlength="12" size="12"/> <br/>
Last Name:<input type="text" name="identifier" maxlength="36" size="12"/> <br/>
<!-- number:<input type="number" name="number" maxlength="36" size="12"/> <br/> -->
<textarea wrap="physical" cols="20" name="description" rows="5">Enter your favorite quote!</textarea><br/>
<p><input type="submit" /></p>
</form>

该帖子不起作用。 JSON 对象创建得很好,将其传递给 API 是个问题。我认为问题就在这里,

data : JSON.stringify($('form').serializeObject()),

如何将上面创建的 JSON 对象传递给数据。谢谢

【问题讨论】:

    标签: javascript jquery json ajax api


    【解决方案1】:

    您不能将POST 和自定义标头与jsonp 一起使用。 jsonp 跨不同域的工作基本上是插入一个 &lt;script&gt; 标记,该标记在完成时调用回调,例如

    <script src="different.domain/api/projects.json?callback=done123"></script>
    
    function done123 (result) {
      // do something with result
    }
    

    服务器(如果它支持jsonp 调用)然后返回如下所示的 JavaScript(不是 JSON!):

    done123({"name1":"val1","name2":{"name3":true,"name4":5}})
    

    完成后调用您的函数并跨域工作,因为它使用script 标记。

    如果您从运行 redmine 的同一域 运行脚本,请将 dataType: 'jsonp' 更改为 json。根据 redmine 期望您发送数据的方式(JSON-body 与 form-data),您可能需要更改 data 值:

    // When redmine API expects JSON post body
    data : JSON.stringify($('form').serializeObject()),
    
    // When redmine API expects multipart POST data
    data : $('form').serializeObject()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多