当我尝试将 JSON 字符串发布到客户端时,出现以下错误。
Uncaught Error: You're trying to decode an invalid JSON String:
<!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 method="post" action="WebForm1.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZDU/UqTLS4JCyWg8lH2WKg+TKxlfMLv46f+TlE5HbZ5k" />
</div>
<div>
</div>
</form>
</body>
</html>
它返回所有 HTML 页面。我的服务器端代码是这样的:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:59145/My%20Application.app/webapp/index.html");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
string json = "{ \"myresultlist\": [ {\"uname\": \"1\",\"pass\": \"anne\" }, { \"uname\": \"2\", \"pass\": \"jack\" }, {\"uname\": \"3\", \"pass\": \"Tom\" } ]}";
httpWebRequest.ContentLength = json.Length;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
有人有什么想法吗?如何仅向客户端发送 JSON 字符串?