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 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真