2012-10-10 17 views
3

当我尝试将JSON字符串发布到客户端时,出现以下错误。Sencha Touch Error您尝试解码无效的JSON字符串HTML页面返回在ASP.NET中

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字符串到客户端?

回答

2

的解决方案如下。

错误的原因是服务器端后期操作。

当我用这个块更改代码时,我得到了json字符串。

Response.Expires = 0; 
Response.ContentType = "application/json"; 
     Response.Write("{ \"myresultlist\": [ {\"uname\": \"1\",\"pass\": \"anne\" }, { \"uname\": \"2\", \"pass\": \"jack\" }, {\"uname\": \"3\", \"pass\": \"Tom\" } ]}"); 
     Response.End(); 
0

如果你的客户正在做POST,你的服务器不能返回POST。不要将HTTP方法设置为等于POST

看看这个服务器响应:

HTTP/1.1 200 OK 
Date: Wed, 10 Oct 2012 19:58:46 GMT 
Server: Apache/2.2.22 (Win64) PHP/5.4.3 
Vary: Accept-Encoding,User-Agent 
Content-Encoding: gzip 
Keep-Alive: timeout=5, max=99 
Connection: Keep-Alive 
Transfer-Encoding: chunked 
Content-Type: application/javascript; charset="iso-8859-1" 
相关问题