本文目錄一覽:
- 1、請求一段簡單的js代碼。謝謝
- 2、js原生請求遠程ip庫url得到的數據如何處理?
- 3、mock.js 原生ajax多次請求
- 4、如何用原生js發送jsonp請求
- 5、如何使用原生js實現ajax請求
- 6、如何用javaScript發送一個網頁請求?
請求一段簡單的js代碼。謝謝
script type=”text/javascript”
var arr = [1,3,6,2,7,8,2,5,7,8,0];
var count2 = 0;
var count7 = 0;
for(var i = 0;iarr.length;i++){
if(arr[i] === 2){
console.log(“2出現在第”+i+”個”);
count2++;
}
if(arr[i] === 7){
console.log(“7出現在第”+i+”個”);
count7++;
}
}
console.log(“2出現了”+count2+”次”);
console.log(“7出現了”+count7+”次”);
/script
js原生請求遠程ip庫url得到的數據如何處理?
這個不是一個數組嗎?將它賦值給一個變數,那麼a[0]就是中國,其它根據數組索引就可以獲取到了。不知道你想要獲取到什麼樣的數據,可以獲取到數組的每個值,然後就可以自己組合數據了。
mock.js 原生ajax多次請求
1. 檢查Mock.mock當中的數據模板定義是否正確。如:”number|+1″,Mock.mock(‘@date’)等帶有隨機數據。
2. 檢查數據返回部分是否根據參數不同返回不同的數據。如:pageList=mockList.filter(…)。
如何用原生js發送jsonp請求
// 引入進去
script
function ajax(options) {
options = options || {};
options.type = (options.type || “GET”).toUpperCase();
options.dataType = options.dataType || ‘json’;
options.async = options.async || true;
options.timeout=options.timeout||8000;//超時處理,默認8s
var params = getParams(options.data);
var timeoutFlag=null;
var xhr;
var that=this;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject(‘Microsoft.XMLHTTP’)
}
xhr.onreadystatechange = function() {
if(options.dataType === ‘json’){
if (xhr.readyState == 4) {
window.clearTimeout(that.timeoutFlag);
var status = xhr.status;
if (status = 200 status 300) {
// 如果需要像 html 表單那樣 POST 數據,請使用 setRequestHeader() 來添加 http 頭。
options.success options.success(xhr.responseText, xhr.responseXML);
} else {
options.fail options.fail(status);
}
}
} else {
if (xmlHttp.readyState == 4 xmlHttp.status == 200) {
window.clearTimeout(that.timeoutFlag);
var oScript = document.createElement(‘script’);
document.body.appendChild(oScript);
var callbackname = ‘ajaxCallBack’
oScript.src = options.url + “?” + params+’callback=’+callbackname;
window[‘ajaxCallBack’] = function(data) {
options.success(data);
document.body.removeChild(oScript);
};
}
}
};
if (options.type == ‘GET’) {
xhr.open(“GET”, options.url + ‘?’ + params, options.async);
xhr.send(null)
} else if (options.type == ‘POST’) {
xhr.open(‘POST’, options.url, options.async);
if(options.contentType==”undefined”||options.contentType==null){
xhr.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
xhr.send(params);
}else{
xhr.setRequestHeader(‘Content-Type’, options.contentType);
xhr.send(JSON.stringify(options.data));
}
}
this.timeoutFlag=window.setTimeout(function(){//計時器,超時後處理
window.clearTimeout(that.timeoutFlag);
//options.fail(“timeout”);
xhr.abort();
}.bind(this),options.timeout);
}
function getParams(data) {
var arr = [];
for (var param in data) {
arr.push(encodeURIComponent(param) + ‘=’ + encodeURIComponent(data[param]));
}
return arr.join(”);
}
/script
// 使用
script
ajax({
url: “”, //請求地址
type: ‘GET’, //請求方式
async:true,//同步非同步設置
timeout:8000,//超時設置
data: {
userName:$(“#username”).val(),
phoneNumber:$(“#userphone”).val(),
orderType:’8′,
requirementDetail:”,
method:’homedecapi.decOrder.insertDecOrder’,
orderSource:’無憂居官網PC’
}, //請求參數
success: function(response, xml) {
if(JSON.parse(response).decOrder_insertDecOrder_response){
// alert(“預約成功”)
$(“#mypopup”).css(‘display’,’block’)
}else{
alert(“預約失敗”)
}
},
fail: function(status) {
console.log(‘狀態碼為’ + status); // 此處為請求失敗後的回調
}
});
/script
如何使用原生js實現ajax請求
var url = “${ctx}/sceneView/dataSetPeriod?sceneId=”+$(“#sceneId”).val()+”dataSetPeriod=”+$(“#dataSetPeriodIdSelect”).val(); //要訪問的url地址,?後面的是要帶回去的參數
$.ajax({
type : “POST”, //設置提交方式 get 或post
url : url, //就是上面的url
dataType : “json”, //提交的數據類型
success : function(msg){
//這個success方法中是你的java類返回的可以用msg點出來,如果要什麼提示信息可以在這裡設置
}
});
如何用javaScript發送一個網頁請求?
看你跟其它用戶的交流,大概的意思就是在打開頁面的時候,發送一次頁面請求,請看代碼:
!DOCTYPE HTML
html
head
meta charset=”UTF-8″/
titleDemoJavascript/title
/head
script type=”text/javascript” src=”
body
div
This is your code.
/div
/body
script type=”text/javascript”
$(document).ready(function(){
var url = ‘htttp://
// 這是你要發送請求的URL地址
setTimeout(function(){
// post前需要引入jQuery庫
$.post(url, {data:data}, function(r){ // data是你發送請求時傳遞的參數(Json格式)
/**
* function裡面的r是你發送請求後,返回的參數
* 比如你發送請求後,返回status=1,info=’Hello World!’;
* 返回後輸出格式:
* if(r.status == 1){
* alert(r.info);
* }else{
* …..
* }
*/
}, ‘json’);
}, 1000); // 1000是指:打開頁面後1秒鐘執行function裡面的操作
});
/script
/html
代碼內的鏈接地址被過濾掉了,你看看下面這張圖
完整代碼,可直接貼用!
原創文章,作者:HFKV,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/134735.html