fetch('https://mywebsite.com/endpoint/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: 'yourValue',
pass: 'yourOtherValue',
})
}).then((response) => response.json())
.then((res) => {
console.log(res);
})
.catch((error) => {
console.warn(error);
});
function toQueryString(obj) {
return obj ? Object.keys(obj).sort().map(function (key) {
var val = obj[key];
if (Array.isArray(val)) {
return val.sort().map(function (val2) {
return encodeURIComponent(key) + '=' + encodeURIComponent(val2);
}).join('&');
}
return encodeURIComponent(key) + '=' + encodeURIComponent(val);
}).join('&') : '';
}
// fetch
body: toQueryString(obj)
$json = json_decode(file_get_contents('php://input'), true);
var_dump($json['username']);
// 获取 app 进行数据集中处理
if(!function_exists('apache_request_headers') ){
$appName = $_SERVER['app'];
}else{
$appName = apache_request_headers()['app'];
}
// 对 RN fetch 参数解码
if($appName == 'your settings') {
$json = file_get_contents('php://input');
$_POST = json_decode($json, TRUE );
}
var App = {
config: {
api: 'your host',
// app 版本号
version: 1.1,
debug: 1,
},
serialize : function (obj) {
var str = [];
for (var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
},
// build random number
random: function() {
return ((new Date()).getTime() + Math.floor(Math.random() * 9999));
},
// core ajax handler
send(url,options) {
var isLogin = this.isLogin();
var self = this;
var defaultOptions = {
method: 'GET',
error: function() {
options.success({'errcode':501,'errstr':'系统繁忙,请稍候尝试'});
},
headers:{
'Authorization': 'your token',
'Accept': 'application/json',
'Content-Type': 'application/json',
'App': 'your app name'
},
data:{
// prevent ajax cache if not set
'_regq' : self.random()
},
dataType:'json',
success: function(result) {}
};
var options = Object.assign({},defaultOptions,options);
var httpMethod = options['method'].toLocaleUpperCase();
var full_url = '';
if(httpMethod === 'GET') {
full_url = this.config.api + url + '?' + this.serialize(options.data);
}else{
// handle some to 'POST'
full_url = this.config.api + url;
}
if(this.config.debug) {
console.log('HTTP has finished %c' + httpMethod + ': %chttp://' + full_url,'color:red;','color:blue;');
}
options.url = full_url;
var cb = options.success;
// build body data
if(options['method'] != 'GET') {
options.body = JSON.stringify(options.data);
}
// todo support for https
return fetch('http://' + options.url,options)
.then((response) => response.json())
.then((res) => {
self.config.debug && console.log(res);
if(res.errcode == 101) {
return self.doLogin();
}
if(res.errcode != 0) {
self.handeErrcode(res);
}
return cb(res,res.errcode==0);
})
.catch((error) => {
console.warn(error);
});
},
handeErrcode: function(result) {
//
if(result.errcode == 123){
return false;
}
console.log(result);
return this.sendMessage(result.errstr);
},
// 提示类
sendMessage: function(msg,title) {
if(!msg) {
return false;
}
var title = title || '提示';
AlertIOS.alert(title,msg);
}
};
module.exports = App;
App.send(url,{
success: function(res,isSuccess) {
}
})
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有