0

I have been looking through different answers to what might be the same question I am asking, and a lot of the solutions have used PHP or JQuery and I have to do this in JavaScript. Here is my problem (and I'm a real noob, so I'm probably doing something really stupid and that's why it's not working).

I am trying to pass 'code' with a value in a query string appended to the URL. Then get the code to write dynamically to the page after. Right now, I'm receiving the error: "Incorrect code submitted". I don't think I am sending the "code" correctly.

I started a jsfiddle too: http://jsfiddle.net/shannongmac/6Z7NR/

Here is my code:

function loadXMLDoc() {
            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) {
            document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            }
        }
            xmlhttp.open("GET","http://florence.ccs.uconn.edu/~adepalma/cgi-bin/iskm3120/response.cgi",true);
            xmlhttp.send("yowza");

        function addText() {
        var newParagraph = document.createElement('p');
        newParagraph.textContent = textArea.value;
        document.getElementById("myDiv").insertBefore(newParagraph, H2);
    }
}
7
  • The request is not completed due to javascripts same-origin policy ? Commented Nov 16, 2013 at 14:27
  • I'm just getting: Incorrect code submitted (that is the error in dreamweaver, I should say). It's not doing anything when I load in Chrome to try it. Commented Nov 16, 2013 at 14:29
  • I'm getting a same-origin error -> jsfiddle.net/6Z7NR/1 Commented Nov 16, 2013 at 14:30
  • But pasting the url http://florence.ccs.uconn.edu/~adepalma/cgi-bin/iskm3120/response.cgi?name=yowza into the browser it returns "incorrect code", and that's the API, so you're doing something wrong ? Commented Nov 16, 2013 at 14:32
  • This was the code we were given in the assignment, but I see what you are saying, so if it's not accepting it at all, is the code itself wrong? Commented Nov 16, 2013 at 14:34

1 Answer 1

1

You have a couple of problems with your code:

  • You're trying to sent POST data when doing a GET request. Try including your "yowsa" string as part of the url, rather than sending it as POST data. Here's a quick intro to the difference: http://www.w3schools.com/tags/ref_httpmethods.asp
  • You need to send the correct key with your value. Your commented out code shows that you tried "name=yowza.xmp". It looks like you need "code=yowza". (A bit of clever googling turns up http://florence.ccs.uconn.edu/~adepalma/cgi-bin/iskm3120/response.cgi?code=yowza as the correct request you need to make. I'm not sure if that counts as cheating because I haven't seen the assignment).
  • Whilst your requests may work in dreamweaver, you're going to face a problem on the real web, because you're making a request to a different domain. Have a look into Cross-Origin Resource Sharing to find out more: http://www.html5rocks.com/en/tutorials/cors/
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, thank you. fixed the URL and you are correct it works testing it in Dreamweaver, however I still get the same error when I run it through any browser, which is most likely what everyone has said that it is a CORS problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.