AngularJS - 如何发送多维$ http.get()数据 [英] AngularJS - How to send multidimensional $http.get() data

查看:123
本文介绍了AngularJS - 如何发送多维$ http.get()数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个JSON数组,它看起来是这样的:

So I have a JSON array that looks something like this:

var myData = {
    foo : {
        biz : 'baz',
        fig : 'tree'
    }
}

这可以被输入到地址栏,如:

This could be typed into the address bar like:

http://www.mysite.com/index?foo[biz]=baz&foo[fig]=tree

和预期这会工作。

问题是,当我提供这个 myData的对象AngularJS的 $ HTTP 服务,如:

The problem is that when I supply this myData object to AngularJS's $http service like:

$http.get('http://www.mysite.com', {
    data : myData
});

有逃逸查询串与不出现甚至即使它并没有在它是不可思议的方式esaped正确的格式。它看起来是这样的:

It escapes the query string and doesn't appear to even be the right format even if it weren't esaped in the weird way it is. It looks something like:

url?foo=%7B%22biz%22%3A%22baz%22%2C%22fig%22%3A%22tree%22%7D

我怎样才能使这项工作?

How can I make this work?

推荐答案

这实际上是在正确的格式。假设你的后端PHP是,当你做 $ _ GET ['富'] 您将获得%7B%22biz%22%3A%22baz %22%2C%22fig%22%3A%22tree%22%7D 。你看到的奇怪字符,因为角是编码将字符串的URL。这具有发送数据之前完成。如果您键入它作为富[BIZ] =巴兹和放大器;富[图] =树在您的浏览器,浏览器通常urlen codeS自动

This is actually in the right format. Assuming your back-end is PHP, when you do $_GET['foo'] you will get %7B%22biz%22%3A%22baz%22%2C%22fig%22%3A%22tree%22%7D. The strange characters you see are because Angular is url encoding the string for you. This has to be done before transmitting the data. If you type it out as foo[biz]=baz&foo[fig]=tree in your browser, the browser usually urlencodes it automatically.

urldecoding json_decoding 这一点,你会得到你所期望的数据。

After urldecoding and json_decoding this, you will get the data you expect.

$foo = json_decode(urldecode($input), true);

Array
(
    [biz] => baz
    [fig] => tree
)

您就可以访问这些为 $ foo的['BIZ'] $ foo的['图']

You can then access these as $foo['biz'] and $foo['fig']

演示

这篇关于AngularJS - 如何发送多维$ http.get()数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆