ASP.NET环境下XMLHttpRequest中responseText()方法返回值为空问题讨论
2017-06-18 00:25
671 查看
ASP.NET环境下XMLHttpRequest中responseText()方法返回值为空问题讨论
一、问题产生环境:用JavaScript的XMLHttpRequest发送GET请求,请求的数据来自asp.net接口,数据格式为string或json。
代码如下:
二、分析:
1.如果将asp.net接口已本地调试的方法给出,那么xmlHttp.status的值将为0;
2.通过调试,xmlHttp.responseText的返回值为空的原因与接口给出的数据格式无关;
3.最终发现是跨域问题。
三、解决方法:
在配置文件中(Web.config)加入以下代码已解决跨域问题:
查看原文:http://www.cnblogs.com/zhangtingzu/
一、问题产生环境:用JavaScript的XMLHttpRequest发送GET请求,请求的数据来自asp.net接口,数据格式为string或json。
代码如下:
<!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" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title></title> <script src="js/jquery-1.4.2.js" type="text/javascript"></script> <script type="text/javascript"> function btn_click() { var xmlHttp = new XMLHttpRequest(); if(!xmlHttp){ alert("error from create xmlHttp!"); return; } //配置XMLHttpRequest对象 xmlHttp.open("GET", "http://192.168.1.102:8030/get.aspx",true); //设置回调函数 xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { document.getElementById("result").innerHTML = xmlHttp.responseText; } } //发送请求 xmlHttp.send(null); } </script> </head> <body> <input type="button" value="获取系统当前时间" id="btn" onclick="btn_click();" /> <div id="result"> </div> </body> </html>
二、分析:
1.如果将asp.net接口已本地调试的方法给出,那么xmlHttp.status的值将为0;
2.通过调试,xmlHttp.responseText的返回值为空的原因与接口给出的数据格式无关;
3.最终发现是跨域问题。
三、解决方法:
在配置文件中(Web.config)加入以下代码已解决跨域问题:
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" /> </customHeaders> </httpProtocol> </system.webServer>
查看原文:http://www.cnblogs.com/zhangtingzu/
相关文章推荐
- 在asp.net中xmlHttp.responseText的返回值全部是html代码,怎样才能只返回我Response.write的内容?
- 在ASP.NET中XMLHTTP.RESPONSETEXT的返回值全部是HTML代码,怎样才能只返回我RESPONSE.WRITE的内容
- 在asp.net中xmlHttp.responseText的返回值全部是html代码,怎样才能只返回我Respon
- js 无参数、有参数的webservice通信方法 XMLHttpRequest和 $.ajax方式汇总及返回值为json问题
- wp7使用POST请求Asp.net网页时,HttpWebRequest的BeginGetResponse方法抛出NotSupportException异常的解决方法
- ASP.NET WebApi 上传文件时异常 Failed to execute send on XMLHttpRequest 的一个处理方法
- ASP.NET Core Web API处理HttpResponseMessage类型返回值的问题
- XMLHttpRequest对象responseText属性的兼容性问题
- XMLHttpRequest.responseXML.documentElement=null的问题
- ASP.NET MVC中使用AJAX(XMLHttpRequest、Microsoft AJAX Library......) 推荐
- Ajax之二: XMLHttpRequest之POST传递XML (应用环境: Windows + IE + .NET[C#])
- 解决ASP.NET中Type.GetType方法总返回空的问题
- asp.net 中 HttpRequest 中跟URL path 有关的方法和属性 (摘自用怪异的眼光去研究blog)
- asp.net httprequest httpresponse
- 关于ASP.NET中用Response.Write()方法响应导致页面字体变大的问题
- 一个创建XMLHttpRequest对象的js文件(适用asp,php,jsp,asp.net等)
- 系统地学习ASP.NET AJAX(8) - 客户端脚本编程(Sys.Net命名空间下的WebRequestManager、WebRequest、WebRequestExecutor和XMLHttpExecutor)
- asp.net 中请求-响应(request-response)的几种方法
- 关于XMLHttpRequest对象的responseText属性
- 在asp.net中读取XML文件信息的4种方法 (http://www.cnblogs.com/liping19851014/archive/2007/10/16/926137.html)