0

Hi all I have to connect to an external server to retrieve data. They told me to use their script and I have to modify something because it was wrong. Now I ahve a problem when I try to lunch my request. Return me an error into my internet explorer console

SCRIPT10: The data required for the completion of this operation are not yet available.

This is my javascript page, the problem I think is because the query doesn't finish in time to print my result. How can I print the result when they are ready and don't return me error? I have try to comment all my request and leave only the method "open" but the error return me every time. Why??

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
var req = null ;
function sendRequest(){
    var urlStr="www.test.it";
    var xmlString="";
    xmlString+="<?xml version='1.0' encoding='UTF-8'?><some xml>";

    createHTTPRequestObject();
    var resp = getResponseText(urlStr+"?"+xmlString,null);

    var xmlDoc;
    xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
    xmlDoc.async = false;

    xmlDoc.loadXML(resp);
    alert(xmlDoc.xml);
}

function createHTTPRequestObject(){
    req=null ;
    var pXmlreq = false ;
    if (window.XMLHttpRequest) {
        pXmlreq = new XMLHttpRequest();
    } 
    else if (window.ActiveXObject) {
        try{
            pXmlreq = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e1) {
            try{
                pXmlreq = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e2) {
            }
        }
    }
    req = pXmlreq ;
}

function getResponseText(action,query,method,async,contenttype){
    if(method==null){
        method="POST";
    }
    if(async==null){
        async="true";
    }
    if(contenttype==null){
        contenttype = "application/x-www-form-urlencoded";
    }
    req.open(method,action, async);
    req.setRequestHeader("Content-Type", contenttype);
    if(query){
        req.send(query);
    }else{
        req.send();
    }
    return req.responseText ;
}
</script>
</head>
<body>
<input type="button" name="Request" value="Request" onclick="sendRequest();"/>
<div id="content" />
</body>
</html>
8
  • what are you talking about... Commented Mar 6, 2013 at 14:44
  • My problem is: I have an error I think this error returns to me because I have to wait the query result ready @Sebas Commented Mar 6, 2013 at 14:45
  • XMLHttpRequests also work in non-IE browsers. Commented Mar 6, 2013 at 14:47
  • Yes but I have some ActiveXObject @MarcelKorpel Commented Mar 6, 2013 at 14:48
  • Your query string (xmlString in your case) should be encoded with encodeURIComponent. When does it output that error message? Use breakpoints and step over your code to see where it goes wrong. Commented Mar 6, 2013 at 14:52

1 Answer 1

2

You are trying to read the responseText before it is ready. Looks like you are treating a asynchronous call as synchronous. That would be the issue.

Sign up to request clarification or add additional context in comments.

7 Comments

ok but if i comment over the open method and leave only it return me the error why? I have only leave the method open I don't understand why
@AlessandroMinoccheri: Your req.responseText is still null when getResponseText finishes.
I have try to comment the response.. the error return me when I call the open method @MarcelKorpel
@AlessandroMinoccheri: Then set a breakpoint and check the values you pass to req.open (is that urlStr variable the correct one?).
yes is correct because if i copyied it into their demo test page it works fine
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.