使用javascript发送帖子值

时间:2017-07-04 14:46:55

标签: javascript php curl

我有这个卷曲,工作正常,我可以使用Post发送值,这是卷曲代码

$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);
}

我正在尝试像curl一样但使用javascript,我尝试使用此代码,但它不起作用

  
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');	

   

1 个答案:

答案 0 :(得分: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) {});