当以 GET 动词请求时,从 Web 服务方法返回 JSON
我有一个返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须指示客户端开发人员将请求内容类型标头设置为“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.