js note

http://mianshiti.diandian.com/page/3?tag=Javascript

1、 jquery 的 ajax 跨域 jsonp记录

 

JSONP requires that the response be wrapped in some kind of callback function.

So the server should respond with:

someFn({....});

The someFn is passed as part of the request, so the server needs to read it and wrap the data appropriately.

$.ajax({
    dataType: 'jsonp',
    data: { api_key : apiKey },
    jsonp:'callback',
    url: "http://de.dawanda.com/api/v1/" + resource + ".js",
    success: function(data) { console.log(data); },
    error: function(jqXHR, textStatus, errorThrown) { console.log(errorThrown); console.log(textStatus); }});
PrintWriter writer = response.getWriter();String jsonString = json.toString(JSON_SPACING);String callback = request.getParameter("callback");// if callback in URL and is not just the "?" (e.g. from localhost)if (callback != null && callback.length() > 1){
    writer.write(callback + "(" + jsonString + ");");}else{
    writer.write(jsonString);}

 

2、提示

  1. 使用createElement动态创建dom,如有外部资源连接的属性,先进行事件监听再做外部资源赋值,如img,先监听img的load事件,再img.src='test.jpg';

  2. 单页面尤其注意javascript内存泄漏,移除dom时清理dom上的所有事件,尽量使用innerHTML一次更新完dom,使用事件委托

 

3、码代码注意事项

 

  1. function也要是有;结尾;如下代码会发生自调用

g=function(){ return 'a'; }
(function(){ function g(){return 'aa';}}());
alert(g);//a
alert(g());//error,g is string,not function

//上面的代码相当于
g=function(){ return 'a'; }(function(){ function g(){return 'aa';}}());

//为此,为防止自调用要养成在function结束后加;号,如下
g=function(){ return 'a'; };(function(){ function g(){return 'aa';}}());

4、待续...

 

转载于:https://my.oschina.net/homeemail/blog/307128

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值