分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

ajax与jsonp

发布时间:2023-09-06 01:40责任编辑:熊小新关键词:jsjson
 ???var xhr = new XMLHttpRequest() ???????xhr.onreadystatechange = function(){//状态触发事件 ???????if(xhr.readyState == 4){ ???????????if(xhr.status >=200 && xhr.status<300 || xhr.status == 304){ ???????????????console.log(xhr) ???????????}else{ ???????????????console.log(xhr.status) ???????????} ???????} ???} ???xhr.onprogress = function(event){//浏览器接受新数据期间周期性触发 ???????if(event.lengthComputable){ ???????????console.log(event.total) ???????} ???} ???????xhr.open(‘get‘,‘data/cart.json‘,true); ???xhr.setRequestHeader(‘myheader‘,‘myvalue‘)//可以设置自定义头部信息 ???xhr.send(null)//接收一个参数,即要作为请求主体发送的数据,若无,为了浏览器兼容,必须传入null。 ???????//跨域jsonp ???function handleResponse(response){ ???????console.log(response.ip,response.city,response.region_name) ???} ???????var script = document.createElement(‘script‘) //script和img一样有能力不受限制从其他域加载资源 ???script.src = ‘http://freegeoip.net/json/?callback=handleResponse‘; ????document.body.insertBefore(script,document.body.firstChild)

ajax与jsonp

原文地址:https://www.cnblogs.com/wangxi01/p/8400595.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved