-2

我的程序中有以下 dojo 代码,我想将 code、client.key、date_joined 等值作为 json 传递。下面我有我的脚本

<table>
  <tr>
    <td>
       <a href="/api/client/{{ client.key }}">{{ client.name }}</a>
    </td>
    <td>
       <form method="get" action="/api/authorize">
       <input type="hidden" name="redirect_uri" value="http://127.0.0.1:8000/api/client   /{{client.key}}/" />  
       <input type="hidden" name="response_type" value="code" />
       <input type="hidden" name="client_id" value="{{client.key}}" />
       <input type="hidden" name="scope" value="date_joined" /> 
       <input type="submit" value="Request" />
       </form>
    </td>
  </tr>
</table>

我需要帮助将上述代码的参数转换为 json,并且我有以下脚本

<script type="text/javascript">
    $(document).ready(function(){
    $('a.authenticate').click(function(e){
    e.preventDefault();
    var token = this.id.replace("_", "");
    $.ajax({
        url: this.href,
        dataType: "jsonp",

        beforeSend: function(xhr){
                xhr.setRequestHeader("Authorization", "Bearer " + token)
                    },
        success: function(data) {
        var response = [];
        for(key in data){
        response.push(key + ":" + data[key]);
                     }
        alert(response.join('\n'));
                      },
             });
          });
    });
</script>
4

1 回答 1

1
$(document).ready(function () {
        $('a.authenticate').click(function (e) {
            e.preventDefault();
            var token = this.id.replace("_", "");
            $.ajax({
                url: "http://127.0.0.1:8000/api/client",
                dataType: "json",
                data: '{"Client.key":"'+your value+'"}',
                //beforeSend: function(xhr){  
                // xhr.setRequestHeader("Authorization", "Bearer " + token); 
                // },  
                success: function (data) {
                    //var response = [];      
                    //for(key in data){     
                    //response.push(key + ":" + data[key]);   
                    //  alert(response.join('\n'));  
                }
            });
        });
    });

对您的代码稍作修改,如果发送 json 对象提及数据类型为 json 并遵循 json 对象格式。有关 json 对象的更多信息,请尝试http://www.json.org/

于 2012-05-11T07:43:28.817 回答