I'm freakin out trying to find a good solution to my problem.
我拼命地想找到一个解决我问题的好办法。
So I'm using an ajax request to pass some data for filtering purposes.
因此,我使用ajax请求传递一些数据以进行过滤。
Then I use history.js so I can change the url and add a state to the browser, making it possible to go back, go forward etc. This way, I can filter my data with an ajax request or a normal request. So far so good.
然后我使用历史。因此,我可以更改url并向浏览器添加一个状态,使它可以返回,向前走等等。这样,我就可以通过ajax请求或正常请求来过滤我的数据。目前为止一切都很顺利。
My problem is, in the data I pass through the ajax request there is an array:
我的问题是,在我传递ajax请求的数据中有一个数组:
function updateContent(url, state) {
$.ajax({
type: 'GET',
data: {name: state.filter_name, num: state.filter_number,
data: state.filter_data, array: state.filter_array},
url: url,
success: function(msg) {
$('#ajax').html(msg);
}
});
}
this will make a request to this url http: //localhost/Project/Controller/?filter_array%5B%5D=1&filter_array%5B%5D=50&filter_array%5B%5D=70.
这将对这个url发出一个请求://localhost/Project/Controller/?filter_array%5B%5D=1&filter_array%5B%5D=50&filter_array%5B%5D=70。
The problem is that I want to pass this url to the history.pushstate
问题是我想把这个url传递给history.pushstate
If I use jsenconde then build manually the url, the url will be slighter different, will be something like: http://localhost/Project/Controller/?filter_array=["1","50","70"]
如果我使用jsenconde然后手工构建url, url会稍微有所不同,比如:http://localhost/Project/Controller/?filter_array=["1","50","70"]
Although it's more cleaner, I read somewhere that you shouldn't use [] in url's but even if I use this way I will get two types of GET url's, the ajax URL ?filter_array%5B%5D=1&filter_array%5B%5D=50&filter_array%5B%5D=70. and the json url ?filter_array=["1","50","70"].
虽然更简洁,但是我在url中看到了不应该使用[]的地方,但是即使我使用这种方式,我也会得到两种get url的类型:ajax url ?filter_array%5B%5D=1&filter_array%5B%5D=50&filter_array%5B% 5B%5D=70。而json url ?filter_array=["1"、"50"、"70"]。
So I just need a solution that get's me just one type of GET url, so I can use the same url to ajax, and normal requests.
所以我只需要一个解决方案来获取我只是一种获取url,所以我可以使用到ajax的相同url,以及正常的请求。
I hope I had made myself clear..
我希望我已经说得很清楚了。
2 个解决方案
#1
0
The standard way to pass an array of values is to pass the name with []
's repeatedly (url-encoded) as in your first example.
传递值数组的标准方法是像第一个示例那样,重复地传递带有[]s (url编码)的名称。
You need to generate your URL in such a way that it complies with this...
您需要以符合以下条件的方式生成URL。
filter_array[]=1&filter_array[]=2&filter_array[]=3
which, of course, encodes as you showed....
当然,编码显示....
filter_array%5B%5D=1&filter_array%5B%5D=2&filter_array%5B%5D=3
If you stick with this format, your code will be more portable (eg ASP.Net will perform the conversion to List automatically as will many PHP libraries).
如果您坚持这种格式,您的代码将更加可移植(例如ASP)。Net将像许多PHP库一样自动执行到列表的转换)。
Is there any reason you can't generate the url in this format?
有没有什么原因让你不能生成这种格式的url ?
The difference here is that one is using standard forms encoding, the other is converting JSON
这里的区别是,一个是使用标准的表单编码,另一个是转换JSON
#2
0
jQuery is URL encoding the JSON object so that there are no illegal characters. If you want to then retrieve that GET variable using javascript, you will have to pull the data from the URL, URL decode it, then use JSON.parse() to turn it back into a JavaScript object.
jQuery是对JSON对象编码的URL,因此没有非法字符。如果想要使用javascript检索GET变量,就必须从URL中提取数据,URL对其进行解码,然后使用JSON.parse()将其转换为javascript对象。
http://www.w3schools.com/jsref/jsref_encodeuri.asp
http://www.w3schools.com/jsref/jsref_encodeuri.asp
#1
0
The standard way to pass an array of values is to pass the name with []
's repeatedly (url-encoded) as in your first example.
传递值数组的标准方法是像第一个示例那样,重复地传递带有[]s (url编码)的名称。
You need to generate your URL in such a way that it complies with this...
您需要以符合以下条件的方式生成URL。
filter_array[]=1&filter_array[]=2&filter_array[]=3
which, of course, encodes as you showed....
当然,编码显示....
filter_array%5B%5D=1&filter_array%5B%5D=2&filter_array%5B%5D=3
If you stick with this format, your code will be more portable (eg ASP.Net will perform the conversion to List automatically as will many PHP libraries).
如果您坚持这种格式,您的代码将更加可移植(例如ASP)。Net将像许多PHP库一样自动执行到列表的转换)。
Is there any reason you can't generate the url in this format?
有没有什么原因让你不能生成这种格式的url ?
The difference here is that one is using standard forms encoding, the other is converting JSON
这里的区别是,一个是使用标准的表单编码,另一个是转换JSON
#2
0
jQuery is URL encoding the JSON object so that there are no illegal characters. If you want to then retrieve that GET variable using javascript, you will have to pull the data from the URL, URL decode it, then use JSON.parse() to turn it back into a JavaScript object.
jQuery是对JSON对象编码的URL,因此没有非法字符。如果想要使用javascript检索GET变量,就必须从URL中提取数据,URL对其进行解码,然后使用JSON.parse()将其转换为javascript对象。
http://www.w3schools.com/jsref/jsref_encodeuri.asp
http://www.w3schools.com/jsref/jsref_encodeuri.asp