jsonp牵扯到同源策略、跨域等问题,这里不细说了.

实现就是创建动态的script标签来请求后台地址:

示例:

 jsonp('xxx.php', { uid: 1 }, function (res) {
console.log(res)
})

实例:

 jsonp('http://localhost/server.php', { uid: 1 }, function (res) {
console.log(res)
})
     function jsonp (url, params, callback) {
var funcName = 'jsonp' + Date.now() + Math.random().toString().substr(2, 5) if (typeof params === 'object') {
var tempArr = []
for (var key in params) {
var value = params[key]
tempArr.push(key + '=' + value)
}
params = tempArr.join('&')
} var script = document.createElement('script')
script.src = url + '?' + params + '&callback=' + funcName
document.body.appendChild(script) window[funcName] = function (data) {
callback(data)
delete window[funcName]
document.body.removeChild(script)
}
}

最新文章

  1. MySQL的InnoDB索引原理详解
  2. Spring--开山篇
  3. Oracle SQL的硬解析和软解析
  4. js profiler
  5. python多进程共享变量Value使用tips
  6. C标准函数库(常用部分)
  7. CSS居中完全解决方案
  8. oj 1031 random permutation
  9. Magento路径函数getBaseUrl使用方法
  10. C语言redirection
  11. jquery live()只支持css选择器
  12. 使用Linux的命令行工具做简单的文本分析
  13. 实现类似QQ的折叠效果
  14. cocos2dx进阶学习之CCTMXLayer
  15. C语言获取文件SHA1哈希
  16. FS SIP呼叫的消息线程和状态机线程
  17. 一些css3的特效 javascript的window对象 定时器 延时器等ing...
  18. APNs 推送原理及问题
  19. node-express-1
  20. Linux命令:popd

热门文章

  1. tips 前端 背景与元素的透明和模糊
  2. weblogic12c配置免密码启动
  3. webdriver操作iframe标记的编辑器
  4. ReferenceError与undefined的区别
  5. (转)cygwin包管理器apt-cyg
  6. Centos 查看内存
  7. python的加密操作
  8. This template requires a more recent version of the Android Eclipse plugin. Please update from versi
  9. Table is marked as crashed and should be repaire (
  10. centos 部署web项目