简体   繁体   English

在JavaScript / AJAX中传递带有查询字符串中的值的代码并附加到URL

[英]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. 我一直在寻找不同的答案,以解决可能要问的同一问题,而且许多解决方案都使用PHP或JQuery,而我必须使用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. 我正在尝试使用附加到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/ 我也开始了jsfiddle: 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);
    }
}

You have a couple of problems with your code: 您的代码有两个问题:

  • You're trying to sent POST data when doing a GET request. 您正在尝试执行GET请求时发送POST数据。 Try including your "yowsa" string as part of the url, rather than sending it as POST data. 尝试将“ yowsa”字符串包含在网址中,而不是将其作为POST数据发送。 Here's a quick intro to the difference: http://www.w3schools.com/tags/ref_httpmethods.asp 以下是区别的快速介绍: 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". 您注释掉的代码表明您尝试了“ name = yowza.xmp”。 It looks like you need "code=yowza". 看起来您需要“ 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). (有些聪明的Google搜索会显示http://florence.ccs.uconn.edu/~adepalma/cgi-bin/iskm3120/response.cgi?code=yowza作为您需要提出的正确请求。我不确定如果算是作弊,因为我还没有看过作业)。
  • 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. 尽管您的请求可以在Dreamweaver中运行,但由于要向其他域发出请求,因此您将在真实的Web上遇到问题。 Have a look into Cross-Origin Resource Sharing to find out more: http://www.html5rocks.com/en/tutorials/cors/ 查看跨域资源共享以了解更多信息: http : //www.html5rocks.com/zh_CN/tutorials/cors/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM