如何从角度js向web API发送二维数组 [英] How to send bi-dimensional array from angular js to web API
问题描述
我在桌面上运行了两个解决方案:一个是使用angularjs的Web解决方案,并调用另一个解决方案,这是一个基于MVC的Web API,都是用vb.net编写的。除了一件事之外,一切都很完美:当我尝试发送二维时,我得到以下错误:
I have two solutions running on my desktop: One is a web solution using angularjs and calling the other solution which is a web API based on MVC and both are written in vb.net. Everything is working perfectly except one thing: when I tried to send a bi-dimensional I got below error:
The request is invalid. The parameters dictionary contains an invalid entry for parameter 'myBiDimenArray' for method 'System.Web.Http.IHttpActionResult Calculate(System.String[,], Int32)' in 'CustomAPI.TestController'. The dictionary contains a value of type 'System.String[]', but the parameter requires a value of type 'System.String[,]'."
以下是我在角js中的代码:
Below is my code in angular js:
var myBiDimenArray = [
[1, 6],
[5, 2],
[9, 10]
];
console.log(myBiDimenArray); // it logs the array correctly
return $http({
method: "GET",
url: urlBase + "Calculate",
params: {
myBiDimenArray: myBiDimenArray,
intSize: 1,
},
headers: {
'Content-Type': 'application/json',
'X-ApiKey': 'test' ,
'Authorization': 'Basic 123456'
}
});
以下是我的代码Web APi:
Below is my code in Web APi:
<HttpGet>
Public Function Calculate(<FromUri> ByVal myBiDimenArray(,) As String, <FromUri> ByVal intSize As Integer) As IHttpActionResult
....
End Function
有什么建议?
我是新手,我已经在vb.net和JavaScript中搜索了如何创建Bi-Dimen数组,所以我在我的解决方案中实现了上述内容但是错误400被抛出甚至没有访问方法。
更新:
请请注意,这是我在webAPi配置文件中的路径:
Any suggestions?
I am new to this, I have searched for how to create Bi-Dimen Array in vb.net and in JavaScript so I implemented the above in my solutions but error 400 is thrown and method is even not accessed.
Update:
Please note that this is my route in webAPi config file:
config.Routes.MapHttpRoute( _
name:="Test_Calculate", _
routeTemplate:="api/{controller}/{action}/{myBiDimenArray}/{intSize}", _
defaults:=New With {.controller = "Test", .action = "Calculate"} _
)
可能是配置问题吗?
我尝试过:
我已将网络APi方法更改为:
Could it be a configuration problem?
What I have tried:
I have changed the web APi method to this:
<HttpGet>
Public Function Calculate(<FromUri> ByVal myBiDimenArray()() As String, <FromUri> ByVal intSize As Integer) As IHttpActionResult
....
End Function
可以访问该方法,myBiDimenArray长度为3(这是正确的),但内部的每个数组都像String一样,例如:[1 ,6::1个参数而不是数组中的2个参数;
The method could be accessed, myBiDimenArray length was 3 (which is correct), but each array inside was acting like a String, example: "[1,6]" : 1 parameter instead of being 2 params inside an array;
推荐答案
http({
method:GET,
url:urlBase +Calculate,
params:{
myBiDimenArray:myBiDimenArray,
intSize:1,
},
header:{
'内容-Type':'application / json',
'X-ApiKey':'test',
'授权':'基本123456'
}
});
http({ method: "GET", url: urlBase + "Calculate", params: { myBiDimenArray: myBiDimenArray, intSize: 1, }, headers: { 'Content-Type': 'application/json', 'X-ApiKey': 'test' , 'Authorization': 'Basic 123456' } });
以下是我在Web APi中的代码:
Below is my code in Web APi:
<HttpGet>
Public Function Calculate(<FromUri> ByVal myBiDimenArray(,) As String, <FromUri> ByVal intSize As Integer) As IHttpActionResult
....
End Function
有什么建议吗?
我是新来的,我搜索过如何创建在vb.net和JavaScript中的Bi-Dimen数组,所以我在我的解决方案中实现了上述内容,但是抛出了错误400,甚至没有访问方法。
更新:
请注意,这是我在webAPi配置文件中的路线:
Any suggestions?
I am new to this, I have searched for how to create Bi-Dimen Array in vb.net and in JavaScript so I implemented the above in my solutions but error 400 is thrown and method is even not accessed.
Update:
Please note that this is my route in webAPi config file:
config.Routes.MapHttpRoute( _
name:="Test_Calculate", _
routeTemplate:="api/{controller}/{action}/{myBiDimenArray}/{intSize}", _
defaults:=New With {.controller = "Test", .action = "Calculate"} _
)
这可能是配置问题吗?
我尝试过:
我已将Web APi方法更改为:
Could it be a configuration problem?
What I have tried:
I have changed the web APi method to this:
<HttpGet>
Public Function Calculate(<FromUri> ByVal myBiDimenArray()() As String, <FromUri> ByVal intSize As Integer) As IHttpActionResult
....
End Function
可以访问该方法,myBiDimenArray长度为3(这是正确的),但内部的每个数组都像String一样,例如:[1 ,6::1个参数,而不是数组中的2个参数;
The method could be accessed, myBiDimenArray length was 3 (which is correct), but each array inside was acting like a String, example: "[1,6]" : 1 parameter instead of being 2 params inside an array;
这篇关于如何从角度js向web API发送二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!