简体   繁体   English

XMLHttpRequest vs curl不起作用

[英]XMLHttpRequest vs curl doesn't work

today i'm triying make a little xhr in javascript, java, and c# but it doesn't work.... i dont know why 今天,我正在用javascript,java和c#进行一些xhr的操作,但是它不起作用...。我不知道为什么

Here is the code 这是代码

var xhr = new XMLHttpRequest();

function init(){
    xhr.open("POST","http://www.opsu.gob.ve/portal/controles/ctrl_portal_cpnev_csni.php",false);
xhr.setRequestHeader("Cookie","SERVERID=webserver4");
xhr.setRequestHeader("Origin","http://www.opsu.gob.ve");
xhr.setRequestHeader("Accept-Encoding","gzip, deflate");
xhr.setRequestHeader("Accept-Language","es-419,es;q=0.8");
xhr.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36");
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.setRequestHeader("Accept","*/*");
xhr.setRequestHeader("Cache-Control","max-age=0");
xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
xhr.setRequestHeader("Connection","keep-alive");
xhr.setRequestHeader("Referer","http://www.opsu.gob.ve/portal/principal.html?ir=cpnev_csni&tp=1&ci=24522469");
xhr.send("ci=24522469&tipo=1");
console.log(xhr.responseText);
}

init();

it just stay it and don't anything.... (I try with async true and the same thing happen) 它只是留下来,什么都没有。...(我尝试使用异步true,发生同样的事情)

but in curl... it works 但是在卷曲中...有效

Here is the line in curl 这是卷曲的线

curl "http://www.opsu.gob.ve/portal/controles/ctrl_portal_cpnev_csni.php" -H "Cookie: SERVERID=webserver4" -H "Origin: http://www.opsu.gob.ve" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: es-419,es;q=0.8" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36" -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: */*" -H "Cache-Control: max-age=0" -H "X-Requested-With: XMLHttpRequest" -H "Connection: keep-alive" -H "Referer: http://www.opsu.gob.ve/portal/principal.html?ir=cpnev_csni&tp=1&ci=24522469" --data "ci=24522469&tipo=1" --compressed

it should return that 它应该返回

5334143;-;2010;-;RICARDO;-;ALEJANDRO;-;MARCANO;-;QUINTANA;-;24522469;-; 5334143;-; 2010;-; RICARDO;-; ALEJANDRO;-; MARCANO;-; QUINTANA;-; 24522469;-;

but don't (with curl it does that) 但是不要(用curl可以做到)

what's wrong in my code? 我的代码有什么问题?

you know any way to do that in c#, java or javascript? 你知道用c#,java或javascript做到这一点的任何方法吗?

why i don't use curl? 为什么我不使用卷发? well i try to get all the users registered there and it's over than 5.000.000 and do that with curl is very complicated.... 好吧,我尝试让所有用户在那里注册,并且超过5.000.000,并且用curl做到这一点非常复杂。

I apreciate your help... 非常感谢您的帮助。

XMLHTTP can't be synchronous, change the last parameter of the open to true. XMLHTTP无法同步,请将open的最后一个参数更改为true。

As this, when your console.log is executed, the request isn't done this, to overcome this you need to set a callback with onreadystatechange, eg: 这样,在执行console.log时,请求并未完成,要解决此问题,您需要使用onreadystatechange设置回调,例如:

 xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200) {
                console.log(xhr.responseText)
            }
        }

http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

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

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