带有WebAPI的AngularJS将JSON参数传递给$ http.get

时间:2016-01-06 19:44:20

标签: angularjs json asp.net-web-api get

问题如下:

我想使用get with body来使用RESTful服务。出于测试目的,我正在使用Fiddler Web Debugger。

现在,向Fiddler提出以下GET请求会得到我期望的结果:

GET http://localhost:3787/TerminologyService/Autosuggest/ HTTP/1.1
Host: localhost:3787
Content-Length: 215
Content-Type: application/json

{ 
"Text": "war",
"Count": "100",
"MedicationWeight": "7",
"ActivityWeight": "0",
"DiseaseWeight": "0",
"GeneWeight": "0",
"SymptomWeight": "0",
"AnatomyWeight": "0",
"OrderingType": "CATEGORY_DIVERSITY"
}

所以现在我使用$ http.get做同样的事情。 以下是我到目前为止的情况:

function getTerms(text, count, medWeight, actWeight, disWeight, genWeight, symWeight, anaWeight, orderingType)
    {
        var config = {
            headers: {'Content-Type' : 'application/json'},
            params: {Text : text,
                            Count : count,
                            MedicationWeight : medWeight,
                            ActivityWeight : actWeight,
                            DiseaseWeight : disWeight,
                            GeneWeight : genWeight,
                            SymptomWeight : symWeight,
                            AnatomyWeight : anaWeight,
                            OrderingType : orderingType}
        }

        return $http.get(
            'http://localhost:3787/TerminologyService/Autosuggest', config);
    }

这确实形成了一个GET网址:

http://localhost:3787/TerminologyService/Autosuggest?ActivityWeight=0&AnatomyWeight=0&Count=10&DiseaseWeight=0&GeneWeight=0&MedicationWeight=7&OrderingType=CATEGORY_DIVERSITY&SymptomWeight=0&Text=war

不幸的是,这会导致网络服务出现错误500。

当我检查由$ http.get调用生成的Fiddler中捕获的流量时,我发现JSON数据没有作为正文传递(显然,因为它是在URL中传递的)。所以我无法得到我在Fiddler首次测试的内容

非常感谢任何帮助

2 个答案:

答案 0 :(得分:1)

<强>更新

通过将GET更改回POST来解决问题。

我错误地认为我应该使用GET

答案 1 :(得分:0)

你不应该期待你的后端GET请求中的正文,但是为了回答你的问题,如$http docs中所述,你可以将请求的正文消息传递给配置对象的data属性。因此,请使用data代替params,它应该具有您想要的行为。

相关问题