“Wie man Curl in JavaScript ausführt” Code-Antworten

Wie man Curl in JavaScript ausführt

#Since javascript already has functions for this purpose
#so it is not advisable to load an extra library for that
#you can achieve the same in native javascript like this
var url = "http://www.google.com";

var xhr = new XMLHttpRequest();
xhr.open("GET", url);

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

xhr.send();

#But still if you want to do it using curl in Node.js, it is possible
#Take a look at: https://github.com/JCMais/node-libcurl
var Curl = require( 'node-libcurl' ).Curl;

var curl = new Curl();

curl.setOpt( 'URL', 'http://www.google.com' );
curl.setOpt( 'FOLLOWLOCATION', true );

curl.on( 'end', function( statusCode, body, headers ) {

    console.info( statusCode );
    console.info( '---' );
    console.info( body.length );
    console.info( '---' );
    console.info( this.getInfo( 'TOTAL_TIME' ) );

    this.close();
});

curl.on( 'error', function ( err, errCode ) {

    //do something

    this.close();
});

curl.perform();

Ahmed

So greifen Sie in JavaScript auf Curl -Daten zu

var url = "www.yahoo.com";

var xhr = new XMLHttpRequest();
xhr.open("GET", url);

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

xhr.send();
Anthony Smith

Ähnliche Antworten wie “Wie man Curl in JavaScript ausführt”

Fragen ähnlich wie “Wie man Curl in JavaScript ausführt”

Weitere verwandte Antworten zu “Wie man Curl in JavaScript ausführt” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen