GVKun编程网logo

来自XMLHttpRequest的空responseText(来自xml数据导入是什么意思)

14

在本文中,我们将为您详细介绍来自XMLHttpRequest的空responseText的相关知识,并且为您解答关于来自xml数据导入是什么意思的疑问,此外,我们还会提供一些关于ajaxxmlHttp

在本文中,我们将为您详细介绍来自XMLHttpRequest的空responseText的相关知识,并且为您解答关于来自xml数据导入是什么意思的疑问,此外,我们还会提供一些关于ajax xmlHttp.responseText、ajax XMLHttpRequest responseText返回值、ajax – 从XMLHttpRequest responseText获取纯文本、ajax-(myXmlHttpRequest.responseXML返回值为空问题的有用信息。

本文目录一览:

来自XMLHttpRequest的空responseText(来自xml数据导入是什么意思)

来自XMLHttpRequest的空responseText(来自xml数据导入是什么意思)

我写了一个XMLHttpRequest,它可以正常运行,但是返回一个空的responseText。

javascript如下:

  var anUrl = "http://api.xxx.com/rates/csv/rates.txt";  var myRequest = new XMLHttpRequest();  callAjax(anUrl);  function callAjax(url) {     myRequest.open("GET", url, true);     myRequest.onreadystatechange = responseAjax;                 myRequest.setRequestHeader("Cache-Control", "no-cache");     myRequest.send(null);  }  function responseAjax() {     if(myRequest.readyState == 4) {        if(myRequest.status == 200) {            result = myRequest.responseText;            alert(result);            alert("we made it");        } else {            alert( " An error has occurred: " + myRequest.statusText);        }     }  }

代码运行正常。我可以遍历,得到readyState == 4和status == 200,但是responseText始终为空。

我收到错误调度:getProperties的日志错误(在Safari调试中),我似乎找不到对其的引用。

我已经在本地和远程服务器上的Safari和Firefox中运行了代码。

放入浏览器时,URL将返回字符串并给出200的状态码。

我在运行良好的Mac Widget中为相同的URL编写了类似的代码,但是在浏览器中使用相同的代码永远不会返回结果。

答案1

小编典典

http://api.xxx.com/您网域的一部分吗?否则,您将被同一原始策略阻止。

您可能想查看以下堆栈溢出帖子,以获取一些可能的解决方法:

ajax xmlHttp.responseText

ajax xmlHttp.responseText

xmlHttp.responseText

在写一个AJAX小实例的时候,发现一个奇怪的问题:

< htmlxmlns = " http://www.w3.org/1999/xhtml " >
< head >
< title >时间显示 < / title>
< / head>
< body >
< scripttype = " text/javascript " >
function ajaxFunction()
{
var xmlHttp;
if (window.XMLHttpRequest)
{
// codeforallnewbrowsers
xmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
codeforIE5andIE6 new ActiveXObject( " Microsoft.XMLHTTP " );
}
if (xmlHttp != null )
{
xmlHttp.onreadystatechange
= function ()
{
if (xmlHttp.readyState == 4 )
{
document.myForm.time.value
= xmlHttp.responseText;
}
}
xmlHttp.open(
" GET " , " Test.aspx " , true );
xmlHttp.send(
null );
}
}
< / script>
< formname = " myForm " >
用户:
< inputtype = " text " name = " username " onkeyup = " ajaxFunction(); " / >
时间: < inputtype = " text " name = " time " / >
< / form>
< / body>
< / html>

Test.aspx的c#代码为:

protected void Page_Load(objectsender,EventArgse)
{
Response.Expires
= - 1 ;
Response.Write(DateTime.Now.ToString());
// 输出当前时间
}

通过xmlHttp.responseText返回的时间却是:当前时间和Test.aspx页面的HTML代码。

有人说,要清除Test.aspx页面上的所有HTML代码,这样返回的确实只有当前时间了。

偶然发现,在Response.Write后面加一句:Response.End(); 就能避免返回Test.aspx页面的HTML代码了,而无需清除页面的HTML。


Response.End();
}

ajax XMLHttpRequest responseText返回值

ajax XMLHttpRequest responseText返回值

总结

以上是小编为你收集整理的ajax XMLHttpRequest responseText返回值全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

ajax – 从XMLHttpRequest responseText获取纯文本

ajax – 从XMLHttpRequest responseText获取纯文本

任何人都可以告诉我如何从 AJAX响应中提取Struts动作类返回的字符串?以下是我的代码片段:

JS电话:

xmlhttp=new XMLHttpRequest();
    xmlhttp.open('POST','getMessage.do',false);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send();
    alert(xmlhttp.responseText);

在struts.xml

<action name="getMessage"method="getMessage"/>

行动

public String getMessage() {
    String msg = null;
    HttpSession hSession = this.request.getSession(false);
    if(null != hSession) {
        if(null != hSession.getAttribute("user")) {
            User user = (User) hSession.getAttribute("user");
            if(null != user.account) {
                msg =  user.account.getMessage(); //Sample message
            }
        }
    }
    return msg;
}

当我打印响应文本(使用警报)时,它打印包含所有HTML信息的消息.实际消息以粗体突出显示

回复消息

html> head> title> Apache Tomcat / 5.0.28 – 错误报告/标题>样式>! – {font-family:Tahoma,Arial,sans-serif;颜色:白色;背景颜色:#525D76;字体大小:22px;} H2 {font-family:Tahoma,sans-serif; color:white; background-color:#525D76; font-size:16px;} H3 {font-family:Tahoma,sans-serif;颜色:白色;背景颜色:#525D76;字体大小:14px;} BODY {font-family:Tahoma,sans-serif;颜色:黑色;背景颜色:白色;} B {font-family:Tahoma,sans-serif;颜色:白色;背景颜色:#525D76;} P {font-family:Tahoma,sans-serif; background:white; color:black; font-size:12px;} A {颜色:黑色;} A.name {颜色:黑色;} HR {颜色:#525D76;} – > / style> / head> body>> HTTP状态404 – 没有为操作com.sample.SampleAction定义的结果$$EnhancerBycglib $$69b4e30e和结果示例messageHR size =“1”noshade =“noshade”> p> b> type / b> ;状态报告/ p> p> b>消息u>没有为动作com.sample.SampleAction定义的结果$$EnhancerBycglib $$69b4e30e和结果示例消息/ u> / p> p> b> description / b> u>请求的资源(没有为动作com.sample.SampleAction $$EnhancerBycglib $$69b4e30e和结果示例消息定义的结果)不可用./u\u0026gt;/p\u0026gt;HR size =“1”noshade =“noshade”> h3> Apache Tomcat / 5.0.28 / h3> / body> html>

解决方法

这样做是这样的..

AJAX CALL

var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else { // code for IE6,IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
 alert(xmlhttp.responseText);
}
}
xmlhttp.open("POST","URL");
xmlhttp.send();

行动

public String execute() throws Exception {
      try{
            PrintWriter outWriter = null;
            StringBuffer msg= new StringBuffer("");
            HttpServletResponse httpResponse = ServletActionContext.getResponse();
            try {
                outWriter = httpResponse.getWriter();
                                        msg.append("String to be sent to View");
                    }

            } catch (IOException e) {
                // Todo Auto-generated catch block
                e.printstacktrace();
            }
            finally{

                if(outWriter!=null){
                    httpResponse.setContentType("text/html");
                    outWriter.println(msg.toString());
                    outWriter.flush();
                    outWriter.close();
                }
            }

        }catch (Exception e) {
            throw new Exception(e);
        }
        return null;
        }

STRUTS.XML中定义的行动

<action name="MYActionName"method="execute">
            <result type="stream">
                    <param name="contentType">text/html</param>
                    <param name="inputName">inputStream</param>
            </result>
        </action>

ajax-(myXmlHttpRequest.responseXML返回值为空问题

ajax-(myXmlHttpRequest.responseXML返回值为空问题

ajaxphpjshtml

代码是这样的:
showcities.php:

             <meta http-equiv="content-type" content="text/html; charset=utf-8"><script type="text/javascript">function getXmlHttpObject(){    var xmlHttpRequest;    if(window.ActiveXObject){        xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");    }else{        xmlHttpRequest=new XMLHttpRequest;    }    return xmlHttpRequest;}    var myXmlHttpRequest="";    function getCities(){        myXmlHttpRequest=getXmlHttpObject();        if(myXmlHttpRequest){            var url="/ajax/showCitiesPro.php";            var data="province="+$(''sheng'').value;            myXmlHttpRequest.open("post",url,true);            myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");            myXmlHttpRequest.onreadystatechange=chuli;            myXmlHttpRequest.send(data);        }    }    function chuli(){        if(myXmlHttpRequest.readyState==4){            if(myXmlHttpRequest.status==200){                window.alert(myXmlHttpRequest.responseXML);            }        }    }    function $(id){        return document.getElementById(id);    }</script><select id="sheng" onchange="getCities();"><!--onchange="getCities();"--><option value="">---省---</option>
<option value="zhejiang">浙江</option>
<option value="jiangsu">江苏</option></select><select id="city"><option value="">--城市--</option></select><select id="county"><option value="">--县城--</option></select>
登录后复制

服务器代码是这样的:
showCityPro.php:

 <?php header("Content-Type:text/xml;charset=utf-8");    header("Cache-Control: no-cache");    //接收用户选择的省    $province=$_POST[''province''];    echo "接收到的数据是:";    ?>    window.alert(myXmlHttpRequest.responseXML);接收到的一直是null
登录后复制
这是为什么?新手,所以没啥悬赏。。求大神帮助。我快被这个错误折磨致死了。。。
登录后复制

今天关于来自XMLHttpRequest的空responseText来自xml数据导入是什么意思的讲解已经结束,谢谢您的阅读,如果想了解更多关于ajax xmlHttp.responseText、ajax XMLHttpRequest responseText返回值、ajax – 从XMLHttpRequest responseText获取纯文本、ajax-(myXmlHttpRequest.responseXML返回值为空问题的相关知识,请在本站搜索。

本文标签: