shannonmac
shannonmac

Reputation: 17

Passing code with a value in a query string appended to the URL in JavaScript/AJAX

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);
    }
}

Upvotes: 0

Views: 118

Answers (1)

lucas
lucas

Reputation: 2016

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/

Upvotes: 1

Related Questions