我已发送Json到Web方法。虽然web方法没有收到json作为字符串。在Webmethod中接收jsonQuery字符串
POST方法Test1.aspx - 在Test3.aspx
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:49633/Test3.aspx/Get");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"d\":{\"accessKey\":\"Chennai\",\"channelId\":\"1025\"}}";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
return responseText;
}
Web方法
[System.Web.Services.WebMethod]
public static string Get(string d)
{
return d;
}
性反应 -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form name="form1" method="post" action="Booking.aspx" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTg3NjI4NzkzNmRkOvsswBe8G74mfKP2QBvs0WW2jms=" />
<div>
<span id="Label3"><font color="Fuchsia">Responce:-</font></span>
<span id="Label1">Label</span>
</div>
</form>
</body>
</html>
QUERY -
我收到HTML页面的响应。但是,我想收到这点我是通过POST方法发送
JSON字符串的JSON -
{"d":{"accessKey":"Chennai","channelId":"1025"}}
编辑
我已经改变了JSON字符串{ “ACCESSKEY”:” Chennai“,”channelId“:”1025“},并更改web方法获取字符串。
[System.Web.Services.WebMethod]
public static string Get(string accessKey, string channelId)
{
return accessKey + channelId;
}
我收到了正确的channelId,accesskey值。但我的原始字符串非常大。所以我需要接收通过POST方法发送的相同的Json字符串。 bzs我只接收部分。一次我的客户端通过调用此Webs方法发送Json字符串。谢谢。
编辑2-
作为每MEZ说我包括ScriptMethod。但它通过远程服务器返回一个错误:(500)内部服务器错误。
[System.Web.Services.WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string Get(string d)
{
return d;
}
考虑使用HTTP通用处理程序。查看链接:http://stackoverflow.com/questions/3702959/asp-net-generic-http-handler-ashx-supporting-jsonp。它会避免发回你不需要的内容,比如你的情况正在发生什么... – Mez
当我更改字符串为{“accessKey”:“Chennai”,“channelId”:“1025”}和webmethod为public静态字符串获取(string accessKey,字符串channelId)我可以得到的值。但我想要一个Json Full String ,. – Sagotharan
您还缺少ScriptMethod响应格式... – Mez