js代码原生请求,js原生get请求

发布时间:2022-11-24

本文目录一览:

  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; i < arr.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) {
                    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);
        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){
            $("#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();
$.ajax({
    type : "POST",
    url : url,
    dataType : "json",
    success : function(msg){
    }
});

如何用javaScript发送一个网页请求?

看你跟其它用户的交流,大概的意思就是在打开页面的时候,发送一次页面请求,请看代码:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>DemoJavascript</title>
</head>
<body>
<div>This is your code.</div>
</body>
<script type="text/javascript">
$(document).ready(function(){
    var url = 'htttp://';
    setTimeout(function(){
        $.post(url, {data:data}, function(r){
            /**
             * function里面的r是你发送请求后,返回的参数
             * 比如你发送请求后,返回status=1,info='Hello World!';
             * 返回后输出格式:
             * if(r.status == 1){
             *  alert(r.info);
             * }else{
             *  .....
             * }
             */
        }, 'json');
    }, 1000);
});
</script>
</html>

代码内的链接地址被过滤掉了,你看看下面这张图 完整代码,可直接贴用!