我是ASP.net C#的新手,也是这个网站的新手......
我需要将一个JSON值发送到服务器...并将相同的JSON字符串从服务器返回给客户端...
客户端背后的代码:Default.aspx
protected void Button1_Click(object sender, EventArgs e)
{
var postURL = "http://localhost:16967/WebSite7/Default2.aspx";
string json = new JavaScriptSerializer().Serialize(new
{
user = "Foo",
password = "Baz"
});
using (var wb = new WebClient())
{
var data = new NameValueCollection();
data["jsonString"] = json;
var response = wb.UploadValues(postURL, "POST", data);
var responseString = Encoding.Default.GetString(response);
Response.Write(responseString);
}
}
服务器端后面的代码:Default2.aspx
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
Response.Write(Request.Form["jsonString"]); //stuck here
}
}
空白结果将回发到客户端(Default.aspx)
提前致谢...
答案 0 :(得分:1)
当请求不是回发时,难道不应该只是执行代码吗? 请记住,当触发控制操作时会发生PostBack。
服务器端后面的代码:Default2.aspx
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Response.Write(Request.Form["jsonString"]); //stuck here
}
}