如何在不编码URL参数的情况下获取骨干网集合?

如何在不编码URL参数的情况下获取骨干网集合?

问题描述:

我们需要发送的请求如何在不编码URL参数的情况下获取骨干网集合?

https://api.github.com/search/repositories?q=angular+user:angular&order=desc 

但在控制台请求

https://api.github.com/search/repositories?q=angular%2Buser%3Aangular&order=desc 

收集

var RepoCollection = Backbone.Collection.extend({ 
    url: 'https://api.github.com/search/repositories', 

    initialize: function() {}, 

    fetch: function(options) { 
     var params = { 
      q:"angular+user:angular", 
      order:"desc" 
     }; 
     return Backbone.Collection.prototype.fetch.call(this, { 
      data: $.param(params) 
     }); 
    } 
}); 

例如:

请求:https://api.github.com/search/repositories?q=com%2Buser%3Attomashuk&order=desc

{ 
    "total_count": 0, 
    "incomplete_results": false, 
    "items": [ 

    ] 
} 

请求:https://api.github.com/search/repositories?q=com+user:ttomashuk&order=desc

{ 
     "total_count": 1, 
     "incomplete_results": false, 
     "items": [ 
     { 
      "id": 104921385, 
      "name": "CompanyOrganizer", 
      "full_name": "ttomashuk/CompanyOrganizer", 
      ......... 
      "score": 1.2680688 
     } 
     ] 
    } 
+1

我不确定你的问题是什么。但是这两个URL是相同的。在第二种情况下,它只是使用标准的url编码。 “+”的网址编码为“%2B”,“:”为“%3A”。 – Poonacha

+0

Poonacha是对的,你面临的错误或问题是什么?看起来你有一个问题,你认为URL编码是源代码,但它可能不是。 –

+1

我对这两个请求都有不同的结果。例如:https://api.github.com/search/repositories?q=com+user:ttomashuk&order=desc { “total_count”:1, “incomplete_results”:false, “items”:[... ...]}和https://api.github.com/search/repositories?q=com%2Buser%3Attomashuk&order=desc { “total_count”:0, “incomplete_results”:false, “items”:[ ] } –

jQuery documentation on $.param显示正是你要找的:

var myObject = { 
 
    a: { 
 
    one: 1, 
 
    two: 2, 
 
    three: 3 
 
    }, 
 
    b: [1, 2, 3] 
 
}; 
 
var recursiveEncoded = $.param(myObject); 
 
var recursiveDecoded = decodeURIComponent($.param(myObject)); 
 

 
console.log("encoded:", recursiveEncoded); 
 
console.log("decoded:", recursiveDecoded);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

应该输出:

encoded: a%5Bone%5D=1&a%5Btwo%5D=2&a%5Bthree%5D=3&b%5B%5D=1&b%5B%5D=2&b%5B%5D=3 
decoded: a[one]=1&a[two]=2&a[three]=3&b[]=1&b[]=2&b[]=3 

所以,你可以取得与:

return Backbone.Collection.prototype.fetch.call(this, { 
    data: decodeURIComponent($.param(params)) 
}); 

你也应该通过任何其他选项最初传递给下取到原取致电所以你首要的是透明的:

fetch: function(options) { 
    options = options || {}; 

    // extend the passed data with the default 
    options.data = decodeURIComponent($.param(_.extend({ 
     q: "angular+user:angular", 
     order: "desc" 
    }, options.data))); 

    return Backbone.Collection.prototype.fetch.call(this, options); 
}