带有数组参数的AngularJS GET ajax调用 [英] AngularJS GET ajax call with Array parameters

查看:26
本文介绍了带有数组参数的AngularJS GET ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到以下SO问题通过发送数组使用 AngularJS 的 $http 服务传递数组的 GET 请求来自 Angular $http POST 的数据.但是那里建议的解决方案似乎对我不起作用.我的是专门关于数组对象.

I have already seen the following SO questions Send array via GET request with AngularJS' $http service and Pass array of data from Angular $http POST. But the suggested solutions there does not seem to work for me. Mine is specifically about array objects.

我有以下带有数组参数的 AngularJS ajax 调用

I have the following AngularJS ajax call with array parameters

return $http({
    method: 'GET',
    url: '/api/PatientCategoryApi/PatCat',
    params: { "numbers[]": [{ First: 999, Second: 888 }]},
    headers: { 'Content-Type': 'application/Json' }
})

该调用生成的 url 似乎对我不起作用.我尝试了各种参数化身,生成的url(使用fiddler得到)如下.

The url generated by that call does not seem to work for me. I have tried various avatars of params, and the url generated (got them by using fiddler) are as follows.

params: { numbers: JSON.stringify([{ First: 999, Second: 888 }]) },
http://localhost:50849/api/PatientCategoryApi/PatCat?numbers=%5B%7B%22First%22:999,%22Second%22:888%7D%5D 

params: { "numbers[]": JSON.stringify([{ First: 999, Second: 888 }]) },
http://localhost:50849/api/PatientCategoryApi/PatCat?numbers%5B%5D=%5B%7B%22First%22:999,%22Second%22:888%7D%5D 

params: { "numbers[]": [{ First: 999, Second: 888 }]},
http://localhost:50849/api/PatientCategoryApi/PatCat?numbers%5B%5D=%7B%22First%22:999,%22Second%22:888%7D 

params: { numbers: [{ First: 999, Second: 888 }]},
http://localhost:50849/api/PatientCategoryApi/PatCat?numbers%5B%5D=%7B%22First%22:999,%22Second%22:888%7D 

我希望网址是 http://localhost:50849/api/PatientCategoryApi/PatCat?numbers[0][first]=999&numbers[0][second]=888.因为这是我的 Asp.net MVC 服务器端代码能够理解和解密数组的唯一方式.

I want the url to be http://localhost:50849/api/PatientCategoryApi/PatCat?numbers[0][first]=999&numbers[0][second]=888. Because this is the only way my Asp.net MVC server side code is able to understand and decipher the array.

一种方法是将 url 本身设置为完全保存参数,如下所示.以下是工作.这是我最后的选择.

One way is to set the url itself to hold the params fully as follows. The following is working. This is the last option for me.

return $http({
    method: 'GET',
    url: '/api/PatientCategoryApi/PatCat?numbers[0][first]=999&numbers[0][second]=888&numbers[1][first]=9999&numbers[1][second]=8888',
    //params: {...}, remove this completely
    headers: { 'Content-Type': 'application/Json' }
})

但我想了解,如何使用参数来做到这一点,以便 AngularJS 会为我做到这一点.

But I want to understand, how to do this using params so AngularJS will do that for me.

推荐答案

你想要 httpParamSerializerJQlike!像这样使用它:

You want the httpParamSerializerJQLike! Use it like this:

$http({
  ...
  params: { "numbers": [{ First: 999, Second: 888 }]},
  paramSerializer: '$httpParamSerializerJQLike',
  ...
});

或者您可以将其设置为配置块中所有 $http 调用的默认值,如下所示:

Or you can set it up as your default for all $http calls in a configuration block, like this:

angular.module('yourModuleName').config(function($httpProvider) {
  $httpProvider.defaults.paramSerializer = '$httpParamSerializerJQLike';
});

这篇关于带有数组参数的AngularJS GET ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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