Angular 7 HTTP GET发送JSON对象作为参数

时间:2019-01-15 18:53:57

标签: json angular http get

我正在尝试通过做类似这样的事情将json结构发送到rest服务

  let test5var = {
                    "test5var1": {
                        "test5var2": "0317",
                        "test5var3": "9556"
                    },
                    "test5var4": "123",
                    "test5var": "0000046"
                }
let dataPrincipalBlnc = {"test": {"test1": {"test2": "0317","test3": {"IDIOMA_ISO": " en","DIALECTO_ISO": "US"},"channel": "INT"},"input": {"test5": test5var}}};

let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');

    let params = new HttpParams().set("requestData", dataPrincipalBlnc.toString()).set("authenticationType", this.authType);


    return this.http.get(this.url, {params: params}); 

请求的结果应如下所示:

https://example.com/test?authenticationType=cookie&requestData=%7B%test%22:%7B%22test1%22:%7B%22test2%22:%220317%22,%22test3%22:%7B%22IDIOMA_ISO%22:%22+en%22,%22DIALECTO_ISO%22:%22US%22%7D,%22channel%22:%22INT%22%7D,%22input%22:%7B%22test5%22:%7B%22test5var1%22:%7B%22test5var2%22:%220317%22,%22test5var3%22:%229556%22%7D,%22test5var4%22:%22123%22,%22test5var5%22:%220000986%22%7D%7D%7D%7D

但当前发送为:

https://example.com/test?requestData=%5Bobject%20Object%5D&authenticationType=cookie

任何想法如何将json对象发送为第一个请求?我需要手动将json转换为有效的uri格式吗?

在angularJS中,仅使用以下代码即可正常工作:

var data = {
      "test1": {
        "test2": {
          "test3": "0317",
          "test4": {
            "IDIOMA_ISO": " en",
            "DIALECTO_ISO": "US"
          },
          "channel": "INT"
        },
        "input": {
          "test5": test5var
        }
      }
};
$http.get(url, {
      params: {
        authenticationType: authType,
        requestData: data
      }
    }).then(success(deferred), error(deferred));

我也尝试使用以下代码,但结果是添加了更多字符,并且后端出现故障,因为它表示JSON格式无效:

encodeURIComponent(JSON.stringify(dataPrincipalBlnc)

requestData =%257B%2522test%2522%253A%257B%2522test1%2522%253A%257B%2522test2%2522%253A%25220317%2522%252C%2522test3%2522%253A%257B%2522IDIO_ISO%2522%253A %2522%2520en%2522%252C%2522DIALECTO_ISO%2522%253A%2522US%2522%257D%252C%2522channel%2522%253A%2522INT%2522%257D%252C%2522input%2522%253A%257B%2522test5%2522%253A %257B%2522test5var1%2522%253A%257B%2522test5var2%2522%253A%25220317%2522%252C%2522test5var4%2522%253A%25229556%2522%257D%252C%2522test5var4%2522%253A%2522123%2522%252C%2522test5var %2522%253A%25220003303%2522%257D%257D%257D%257D&authenticationType = cookie

谢谢 问候

3 个答案:

答案 0 :(得分:4)

任何传递给服务的JSON对象都应通过响应主体发送。 您应该仅在url中添加有效的字符串参数。

大多数浏览器也有url size limitation,因此较大的对象可能会导致您遇到长网址问题。

您正在看到requestData=%5Bobject%20Object%5D&authenticationType=cookie,因为它无法将JSON对象放入url 查询字符串中。

  

某些字符不能成为URL的一部分(例如,空格),而某些其他字符在URL中具有特殊含义:例如,字符#可用于进一步指定a的子部分(或片段)。文献;字符=用于将名称与值分开。查询字符串可能需要转换以满足这些约束。这可以使用称为 URL编码的模式来完成。

当您有JavaScript对象并将其转换为字符串(包含JSON文本)时,请使用JSON.stringify。这称为序列化。

与JSON无关:

只要您想在URL中发送“有问题的”字符(例如&,%等),请使用encodeURIComponent。相反的是decodeURIComponent

还是我希望将对象发送到请求正文中。

因此,在您的情况下使用:

 let params = new HttpParams()
.set("requestData", encodeURIComponent(JSON.stringify(dataPrincipalBlnc)))
.set("authenticationType", this.authType);

答案 1 :(得分:1)

在@nircraft答案中(非常详尽且很好),此实现似乎可以为您解决问题,

let test5var = {
                "test5var1": {
                    "test5var2": "0317",
                    "test5var3": "9556"
                },
                "test5var4": "123",
                "test5var": "0000046"
            }
let dataPrincipalBlnc = '{"test": {"test1": {"test2": "0317","test3": {"IDIOMA_ISO": " en","DIALECTO_ISO": "US"},"channel": "INT"},"input": {"test5": test5var}}}';

let headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');

let params = new HttpParams().set("requestData", encodeURIComponent(dataPrincipalBlnc)).set("authenticationType", this.authType);


return this.http.get(this.url, {params: params}); 

在Javascript中,您基本上可以在''""中包含一个字符串。

当您不特别括起字符串时,我相信它会被""括起来,从而使您的JSON响应在使用stringify时需要转义字符。

用这样的字符串将确保双引号将确保它不需要转义符。

如果您有任何疑问,请告诉我。

答案 2 :(得分:0)

我只是通过将数据定义为对象并仅使用JSON.stringify来解决了这个问题:

let dataPrincipalBlnc: object;

let dataPrincipalBlnc = {"test": {"test1": {"test2": "0317","test3": {"IDIOMA_ISO": " en","DIALECTO_ISO": "US"},"channel": "INT"},"input": {"test5": test5var}}};

let params = new HttpParams().set("requestData", JSON.stringify(dataPrincipalBlnc)).set("authenticationType", this.authType);

感谢您的帮助 问候