当以 GET 动词请求时,从 Web 服务方法返回 JSON

发布于 2024-09-07 04:49:50 字数 633 浏览 2 评论 0原文

我有一个返回 JSON 的 .NET Web 服务。

客户端开发人员现在想要以 GET 方法发送请求,并将参数放入 querystirng 中。

我已在 web.config 文件中启用 GET 动词,并在 ScriptMethod 属性中添加了 UseHttpGet=true,现在我可以接收参数了。

但是当他使用 URL 调用该方法时,他会返回 XML。

谁能告诉我如何使用简单的 URL 来调用该方法并获取 JSON 作为响应?

我的网络服务:

[WebMethod]  
[ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = true)]
public string HelloWorld(string str)
{
    return str;
}

他进行的调用是 http://Mysite/Service/Service.asmx/ HelloWorld?str=hisValue

I have a .NET webservice that is returns JSON.

The client-side developer now wants to send his request in GET method, putting the parameters in the querystirng.

I have enabled the GET verb in my web.config file, I have added UseHttpGet=true in the ScriptMethod attribute, and now I am able to recive the parameters.

BUT when he calls the method using the URL he gets back XML.

Can anyone tell me how he can use a simple URL to invoke the method and get JSON as a response?

My webservice:

[WebMethod]  
[ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = true)]
public string HelloWorld(string str)
{
    return str;
}

the call he makes is http://Mysite/Service/Service.asmx/HelloWorld?str=hisValue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2024-09-14 04:49:50

您必须指示客户端开发人员将请求内容类型标头设置为“application/json”。然后,服务应将结果序列化为 JSON。

You have to instruct your client-side dev to set the request content-type header to 'application/json'. The service should then serialize results as JSON.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文