drtpbx3606 2012-08-29 17:09
浏览 64
已采纳

PHP代理jQuery部分到javascript

Here is code that is done in jQuery and the aim is to change it to javascript. The original code is from here: php proxy

Here is the jQuery to change to javascript:

$(function(){

  // Handle form submit.
  $('#params').submit(function(){
    var proxy = 'ba-simple-proxy.php',
      url = proxy + '?' + $('#params').serialize();

      // Make GET request.
      $.get( url, function(data){

        $('#response')
          .html( '<pre class="brush:xml"/>' )
          .find( 'pre' ) // To make the lines proper and to frame the text.
            .text( data );

      });

    // Prevent default form submit action.
    return false;
  });
});

And below is the same code in javascript, which is not ready yet. The problem is that it is not logical at the moment.

The form bit of html which is not included here calls ajaxcontent(). ajaxcontent() creates the ".html" and ".text" part of the jQuery. JQuery ".find" can be ignored at the moment.

In ajaxcontent() the "pre" tag is created first, then added class "brush:xml" in it. Then "createTextNode" adds the source coude from the page that is asked. This is the "( data )" in jQuery. Here the code fails, since ajax wants to place the information to id="response", but ajaxcontent() still needs to do this line "xelement.appendChild(xtext);" to attach the text to "xelement".

function loadajax() {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
        }
    else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("response").innerHTML=xmlhttp.responseText;
        }
    }

    var urlvar=(document.getElementById("url").value)

    xmlhttp.open("GET","ba-simple-proxy.php&url="+urlvar,true);
    xmlhttp.send();
}

function ajaxcontent() {
    var xelement=document.createElement("pre");
    xelement.className="brush:xml";
    var xtext=document.createTextNode( loadajax() );
    xelement.appendChild(xtext);
}

Any suggestions how to make this work? The jQuery part is a working example but the javascript part is not yet.

  • 写回答

1条回答 默认 最新

  • douze2890241475 2012-08-30 23:45
    关注

    OK here is a Ajax POST without jQuery(and a JSFiddle to see it in action, note that this used JSFiddle echo service which requires that the method be POST which is slightly different):

    <input id="url" value="http://google.com/"/>
    <button id="Button">No jQuery POST</button>
    <div id="response" style="border: dashed 1px black;"></div>
    
    document.getElementById('Button').onclick = function() {
        var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        }
        else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("response").innerHTML = "Responce returned!<br/>Data:" + xmlhttp.responseText;
            }
        }
    
        var params = "html=" + escape(document.getElementById("url").value) + "&delay=3";
        xmlhttp.open("POST", "/echo/html/", true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length);
        xmlhttp.setRequestHeader("Connection", "close");
    
        xmlhttp.send(params);
    }
    

    Now this is why things like jQuery are so nice, they hide all of this from the developer :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog