0

I have this curl that works ok and I can send the values using Post this is the curl code

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://192.168.1.9/apply.cgi");
url_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "submit_button=Wireless_Basic&action=Apply&change_action=gozila_cgi&submit_type=add_vifs&wl0_nctrlsb=&wl1_nctrlsb=&iface=ath0&ath0_mode=wet&ath0_relayd_gw_auto=0&ath0_relayd_gw_ipaddr=4&ath0_relayd_gw_ipaddr_0=192&ath0_relayd_gw_ipaddr_1=168&ath0_relayd_gw_ipaddr_2=1&ath0_relayd_gw_ipaddr_3=1&ath0_net_mode=g-only&ath0_channelbw=20&ath0_ssid=TP-LINK&ath0_regdomain=SPAIN&ath0_txpwrdbm=20&ath0_antgain=0&ath0_protmode=None&ath0_rts=0&ath0_rtsvalue=2190&ath0_preamble=0&ath0_txantenna=7&ath0_rxantenna=7&ath0_ap_isolate=0&ath0_scanlist=default&ath0_distance=2000&ath0_mtikie=0&ath0_bridged=1&ath0_multicast=0&ath0_nat=1&ath0_ipaddr=4&ath0_ipaddr_0=0&ath0_ipaddr_1=0&ath0_ipaddr_2=0&ath0_ipaddr_3=0&ath0_netmask=4&ath0_netmask_0=0&ath0_netmask_1=0&ath0_netmask_2=0&ath0_netmask_3=0");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0";
$headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$headers[] = "Accept-Language: es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$headers[] = "Referer: http://192.168.1.90/Wireless_Basic.asp";
$headers[] = "Authorization: Basic YWRtaW46Q3JlYXRpdmUx";
$headers[] = "Connection: keep-alive";
$headers[] = "Upgrade-Insecure-Requests: 1";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
}

I am trying to do the same as curl but using javascript, I try with this code but It doesn´t works

  
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://192.168.1.90/apply.cgi', true);
xhr.setRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0');
xhr.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
xhr.setRequestHeader('Accept-Language', 'es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3');
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Authorization", "Basic YWRtaW46Q3JlYXRpdmUx");
xhr.setRequestHeader("Upgrade-Insecure-Requests", "1");

xhr.onload = function () {
    // do something to response
    console.log(this.responseText);
};
xhr.send('submit_button=Wireless_Basic&action=Apply&change_action=gozila_cgi&submit_type=add_vifs&wl0_nctrlsb=&wl1_nctrlsb=&iface=ath0&ath0_mode=wet&ath0_relayd_gw_auto=0&ath0_relayd_gw_ipaddr=4&ath0_relayd_gw_ipaddr_0=192&ath0_relayd_gw_ipaddr_1=168&ath0_relayd_gw_ipaddr_2=1&ath0_relayd_gw_ipaddr_3=1&ath0_net_mode=g-only&ath0_channelbw=20&ath0_ssid=TP-LINK&ath0_regdomain=SPAIN&ath0_txpwrdbm=20&ath0_antgain=0&ath0_protmode=None&ath0_rts=0&ath0_rtsvalue=2190&ath0_preamble=0&ath0_txantenna=7&ath0_rxantenna=7&ath0_ap_isolate=0&ath0_scanlist=default&ath0_distance=2000&ath0_mtikie=0&ath0_bridged=1&ath0_multicast=0&ath0_nat=1&ath0_ipaddr=4&ath0_ipaddr_0=0&ath0_ipaddr_1=0&ath0_ipaddr_2=0&ath0_ipaddr_3=0&ath0_netmask=4&ath0_netmask_0=0&ath0_netmask_1=0&ath0_netmask_2=0&ath0_netmask_3=0');	

   

3
  • 1
    if you are willing to use external library i would suggest using Jquery with AJAX, since the syntax is way easier. Commented Jul 4, 2017 at 14:48
  • @Nicolas no need to use a big jquery for just ajax. using something like fetch,request,axios will work too Commented Jul 4, 2017 at 14:50
  • Maybe, i don't know those. Also @htmlpower, what exaclty is not working with your request ? Commented Jul 4, 2017 at 14:51

1 Answer 1

1

With This Library

var api = axios.create({
  baseURL: 'http://192.168.1.90',
  timeout: 1000,
  headers: {'Authorization': 'Basic YWRtaW46Q3JlYXRpdmUx',...}
});
api.post('/apply.cgi', {
...
}).then(function(response) {});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.