﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-孤灯野火-文章分类-JSONP</title><link>http://www.blogjava.net/liudawei/category/55018.html</link><description>畅想的天空</description><language>zh-cn</language><lastBuildDate>Thu, 14 Apr 2016 03:05:56 GMT</lastBuildDate><pubDate>Thu, 14 Apr 2016 03:05:56 GMT</pubDate><ttl>60</ttl><item><title>JSONP不支持循环调用(转CNBLOGS)</title><link>http://www.blogjava.net/liudawei/articles/430089.html</link><dc:creator>孤飞燕</dc:creator><author>孤飞燕</author><pubDate>Thu, 14 Apr 2016 02:52:00 GMT</pubDate><guid>http://www.blogjava.net/liudawei/articles/430089.html</guid><wfw:comment>http://www.blogjava.net/liudawei/comments/430089.html</wfw:comment><comments>http://www.blogjava.net/liudawei/articles/430089.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/liudawei/comments/commentRss/430089.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/liudawei/services/trackbacks/430089.html</trackback:ping><description><![CDATA[<h1><br /></h1><div style="max-width: 100%; margin: 0px; border: 0px; font-family: STHeiti, Helvetica, sans-serif; font-size: 16px; line-height: 22px; clear: both; background-color: #fefef2;"></div><div sm_sf"="" style="max-width: 100%; padding: 5px 2px 5px 5px; margin: 0px; border: 0px; font-family: STHeiti, Helvetica, sans-serif; line-height: 22px; background-color: #fefef2;"><div id="cnblogs_post_body" style="max-width: 100%; margin: 0px; border: 0px;"><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;"><span sm_bf"="" style="max-width: 100%; font-size: 18px;"><span style="max-width: 100%; font-weight: 700;">问题描述</span></span></p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;">　　在jquery或zepto下，循环调用同一个jsonp</p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;">　　</p><div sm_sf"="" style="max-width: 100%; padding: 5px; margin: 0px; border: 1px solid #cccccc; background: #f5f5f5;"><div style="max-width: 100%; margin: 5px 0px 0px; border: 0px;"><span style="max-width: 100%; padding: 0px 5px 0px 0px;"><a href="https://zm12.sm-tc.cn/?src=l4uLj8XQ0IiIiNGckZ2TkJiM0ZyQktCSkJKWkZiajdCP0MvLysbIzs3Rl4uSkw%3D%3D&amp;uid=8e8fa63b921aa162d27b9d4910354d09&amp;restype=1&amp;from=derive&amp;depth=2&amp;link_type=60&amp;v=1" id="uc_click_base_url" shenma_update="http://www.cnblogs.com/mominger/p/4459712.html" style="max-width: 100%; text-decoration: none; color: #0069eb;"></a><a add_param="link_type=260" style="max-width: 100%; text-decoration: underline; color: #075db3;"><img alt="复制代码" src="http://cdn12.sm-img5.com/?src=l4uLj8XQ0JyQkpKQkdGckZ2TkJiM0ZyQktCWkp6YmozQnJCPhpyQm5rRmJaZ&amp;restype=3&amp;from=derive&amp;pi=&amp;v=1" style="max-width: 100%; border: 0px; text-decoration: none;" /></a></span></div><pre style="max-width: 100%; white-space: pre-wrap; word-wrap: break-word;"> <span style="max-width: 100%; color: #0000ff;">for</span>(<span style="max-width: 100%; color: #0000ff;">var</span> i = <span style="max-width: 100%; color: #800080;">0</span>;i&lt;<span style="max-width: 100%; color: #800080;">5</span>;i++<span style="max-width: 100%;">){<br style="max-width: 100%;" />        $.ajax({<br style="max-width: 100%;" />            url:</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;"><span style="max-width: 100%; word-break: break-all;">https://m.suning.com/authStatus?callback=checkLogin1&amp;_=1430100870770</span></span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%;">,<br style="max-width: 100%;" />            dataType:</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">jsonp</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%;">,<br style="max-width: 100%;" />            jsonpCallback:</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">checkLogin1</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%;">,<br style="max-width: 100%;" />            success:function(data){<br style="max-width: 100%;" />                console.info(</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">success</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%;">);<br style="max-width: 100%;" />            },<br style="max-width: 100%;" />            error:function(xhr,e){<br style="max-width: 100%;" />                console.error(e);<br style="max-width: 100%;" />            }<br style="max-width: 100%;" />        });<br style="max-width: 100%;" />    }</span></pre><div style="max-width: 100%; margin: 5px 0px 0px; border: 0px;"><span style="max-width: 100%; padding: 0px 5px 0px 0px;"><a add_param="link_type=260" style="max-width: 100%; text-decoration: underline; color: #075db3;"><img alt="复制代码" src="http://cdn12.sm-img5.com/?src=l4uLj8XQ0JyQkpKQkdGckZ2TkJiM0ZyQktCWkp6YmozQnJCPhpyQm5rRmJaZ&amp;restype=3&amp;from=derive&amp;pi=&amp;v=1" style="max-width: 100%; border: 0px; text-decoration: none;" /></a></span></div></div><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;">　　<span style="max-width: 100%; font-weight: 700;">结果</span></p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;"><img alt="" src="http://cdn12.sm-img5.com/?src=l4uLj8XQ0JaSnpiajNGckZaLnZOQmNGckJLQnZOQmM3PzsrQyc3OzMrL0M3PzsrPy9DNyM7OzsbOysjPx8zHxsjRj5GY&amp;restype=3&amp;from=derive&amp;pi=&amp;v=1" style="max-width: 100%; border: 0px;" /></p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;">　　有些成功有些失败了？这是为何？</p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;">&nbsp;</p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;"><span style="max-width: 100%; font-weight: 700;"><span sm_bf"="" style="max-width: 100%; font-size: 18px;">问题解释</span></span></p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;">　　观察jsonp的源码</p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;">　　</p><div sm_sf"="" style="max-width: 100%; padding: 5px; margin: 0px; border: 1px solid #cccccc; background: #f5f5f5;"><div style="max-width: 100%; margin: 5px 0px 0px; border: 0px;"><span style="max-width: 100%; padding: 0px 5px 0px 0px;"><a add_param="link_type=260" style="max-width: 100%; text-decoration: underline; color: #075db3;"><img alt="复制代码" src="http://cdn12.sm-img5.com/?src=l4uLj8XQ0JyQkpKQkdGckZ2TkJiM0ZyQktCWkp6YmozQnJCPhpyQm5rRmJaZ&amp;restype=3&amp;from=derive&amp;pi=&amp;v=1" style="max-width: 100%; border: 0px; text-decoration: none;" /></a></span></div><pre style="max-width: 100%; white-space: pre-wrap; word-wrap: break-word;"> <span style="max-width: 100%; color: #008000;">/*</span><span style="max-width: 100%; color: #008000;">*<br style="max-width: 100%;" />     * jsonp请求<br style="max-width: 100%;" />     * @param options<br style="max-width: 100%;" />     * @param deferred<br style="max-width: 100%;" />     * @returns {*}<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">*/</span><span style="max-width: 100%;">     $.ajaxJSONP </span>=<span style="max-width: 100%;"> function(options, deferred){<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">未设置type，就走     ajax     让参数初始化.<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">如直接调用ajaxJSONP，type未设置</span>         <span style="max-width: 100%; color: #0000ff;">if</span> (!(<span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">type</span><span style="max-width: 100%; color: #800000;">'</span> <span style="max-width: 100%; color: #0000ff;">in</span> options)) <span style="max-width: 100%; color: #0000ff;">return</span><span style="max-width: 100%;"> $.ajax(options)<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #0000ff;">var</span> _callbackName = options.jsonpCallback,     <span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">回调函数名</span><span style="max-width: 100%; word-break: break-all;">             callbackName = ($.isFunction(_callbackName) ?</span><span style="max-width: 100%;">                 _callbackName() : _callbackName) </span>|| (<span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">jsonp</span><span style="max-width: 100%; color: #800000;">'</span> + (++jsonpID)), <span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">没有回调，赋默认回调</span>             script = document.createElement(<span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">script</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%;">),<br style="max-width: 100%;" />            originalCallback </span>= window[callbackName], <span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">回调函数</span> <span style="max-width: 100%;">            responseData,<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">中断请求，抛出error事件<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">这里不一定能中断script的加载，但在下面阻止回调函数的执行</span>             abort =<span style="max-width: 100%;"> function(errorType) {<br style="max-width: 100%;" />                $(script).triggerHandler(</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">error</span><span style="max-width: 100%; color: #800000;">'</span>, errorType || <span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">abort</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%;">)<br style="max-width: 100%;" />            },<br style="max-width: 100%;" />            xhr </span>=<span style="max-width: 100%;"> { abort: abort }, abortTimeout<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">xhr为只读deferred</span>         <span style="max-width: 100%; color: #0000ff;">if</span><span style="max-width: 100%;"> (deferred) deferred.promise(xhr)<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">监听加载完，加载出错事件</span>         $(script).on(<span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">load error</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%;">, function(e, errorType){<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">清除超时设置timeout</span> <span style="max-width: 100%;"><span style="max-width: 100%; word-break: break-all;">            clearTimeout(abortTimeout)<br style="max-width: 100%;" /></span></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">删除加载用的script。因为已加载完了</span> <span style="max-width: 100%;">            $(script).off().remove()<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">错误调用error</span>             <span style="max-width: 100%; color: #0000ff;">if</span> (e.type == <span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">error</span><span style="max-width: 100%; color: #800000;">'</span> || !<span style="max-width: 100%;">responseData) {<br style="max-width: 100%;" />                ajaxError(</span><span style="max-width: 100%; color: #0000ff;">null</span>, errorType || <span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">error</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%;">, xhr, options, deferred)<br style="max-width: 100%;" />            } </span><span style="max-width: 100%; color: #0000ff;">else</span><span style="max-width: 100%;"> {<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">成功调用success</span>                 ajaxSuccess(responseData[<span style="max-width: 100%; color: #800080;">0</span><span style="max-width: 100%;">], xhr, options, deferred)<br style="max-width: 100%;" />            }<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">回调函数</span>             window[callbackName] =<span style="max-width: 100%;"> originalCallback<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #0000ff;">if</span> (responseData &amp;&amp;<span style="max-width: 100%;"><span style="max-width: 100%; word-break: break-all;"> $.isFunction(originalCallback))<br style="max-width: 100%;" />                originalCallback(responseData[</span></span><span style="max-width: 100%; color: #800080;">0</span><span style="max-width: 100%;">])<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">清空闭包引用的变量值，不清空，需闭包释放，父函数才能释放。清空，父函数可以直接释放</span>             originalCallback = responseData =<span style="max-width: 100%;"> undefined<br style="max-width: 100%;" />        })<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #0000ff;">if</span> (ajaxBeforeSend(xhr, options) === <span style="max-width: 100%; color: #0000ff;">false</span><span style="max-width: 100%;">) {<br style="max-width: 100%;" />            abort(</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">abort</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%;">)<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #0000ff;">return</span><span style="max-width: 100%;"> xhr<br style="max-width: 100%;" />        }<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">回调函数设置，给后台执行</span>         window[callbackName] =<span style="max-width: 100%;"> function(){<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">/*</span><span style="max-width: 100%; color: #008000;"><span style="max-width: 100%; word-break: break-all;">  console.info('callbackName arguments ');<br style="max-width: 100%;" />            console.info(arguments[0]);</span></span><span style="max-width: 100%; color: #008000;">*/</span><span style="max-width: 100%;">             responseData </span>=<span style="max-width: 100%;"> arguments<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">/*</span><span style="max-width: 100%; color: #008000;"><span style="max-width: 100%; word-break: break-all;">console.info('responseData ');<br style="max-width: 100%;" />            console.info(responseData);</span></span><span style="max-width: 100%; color: #008000;">*/</span><span style="max-width: 100%;">         }         </span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">回调函数追加到请求地址</span><span style="max-width: 100%; word-break: break-all;">         script.src = options.url.replace(/\?(.+)=\?/, </span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">?$1=</span><span style="max-width: 100%; color: #800000;">'</span> +<span style="max-width: 100%;"><span style="max-width: 100%; word-break: break-all;"> callbackName)<br style="max-width: 100%;" />        document.head.appendChild(script)<br style="max-width: 100%;" /></span></span><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">超时处理，通过setTimeout延时处理</span>         <span style="max-width: 100%; color: #0000ff;">if</span> (options.timeout &gt; <span style="max-width: 100%; color: #800080;">0</span>) abortTimeout =<span style="max-width: 100%;"> setTimeout(function(){<br style="max-width: 100%;" />            abort(</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">timeout</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%;">)<br style="max-width: 100%;" />        }, options.timeout)<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #0000ff;">return</span><span style="max-width: 100%;"> xhr<br style="max-width: 100%;" />    }</span></pre><div style="max-width: 100%; margin: 5px 0px 0px; border: 0px;"><span style="max-width: 100%; padding: 0px 5px 0px 0px;"><a add_param="link_type=260" style="max-width: 100%; text-decoration: underline; color: #075db3;"><img alt="复制代码" src="http://cdn12.sm-img5.com/?src=l4uLj8XQ0JyQkpKQkdGckZ2TkJiM0ZyQktCWkp6YmozQnJCPhpyQm5rRmJaZ&amp;restype=3&amp;from=derive&amp;pi=&amp;v=1" style="max-width: 100%; border: 0px; text-decoration: none;" /></a></span></div></div><p style="max-width: 100%; padding: 0px; margin: 0px; border: 0px;">　　问题出在多线程处理。&nbsp;当第一个jsonp刚执行完callback，赋了值时，此时，script的load事件还未触发。第二个JSONP开始初始化。然后第一个script的load开始执行，但它的数据已被清掉了</p><p style="max-width: 100%; padding: 0px; margin: 0px; border: 0px;">&nbsp;&nbsp;&nbsp; 第一个jsonp刚执行完callback，响应数据赋给了&nbsp;responseData&nbsp;</p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;">　　</p><div sm_sf"="" style="max-width: 100%; padding: 5px; margin: 0px; border: 1px solid #cccccc; background: #f5f5f5;"><div style="max-width: 100%; margin: 5px 0px 0px; border: 0px;"><span style="max-width: 100%; padding: 0px 5px 0px 0px;"><a add_param="link_type=260" style="max-width: 100%; text-decoration: underline; color: #075db3;"><img alt="复制代码" src="http://cdn12.sm-img5.com/?src=l4uLj8XQ0JyQkpKQkdGckZ2TkJiM0ZyQktCWkp6YmozQnJCPhpyQm5rRmJaZ&amp;restype=3&amp;from=derive&amp;pi=&amp;v=1" style="max-width: 100%; border: 0px; text-decoration: none;" /></a></span></div><pre style="max-width: 100%; white-space: pre-wrap; word-wrap: break-word;"><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">回调函数设置，给后台执行</span>         window[callbackName] =<span style="max-width: 100%;"> function(){<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">/*</span><span style="max-width: 100%; color: #008000;"><span style="max-width: 100%; word-break: break-all;">  console.info('callbackName arguments ');<br style="max-width: 100%;" />            console.info(arguments[0]);</span></span><span style="max-width: 100%; color: #008000;">*/</span><span style="max-width: 100%;">             responseData </span>=<span style="max-width: 100%;"> arguments<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">/*</span><span style="max-width: 100%; color: #008000;"><span style="max-width: 100%; word-break: break-all;">console.info('responseData ');<br style="max-width: 100%;" />            console.info(responseData);</span></span><span style="max-width: 100%; color: #008000;">*/</span><span style="max-width: 100%;">         }</span></pre><div style="max-width: 100%; margin: 5px 0px 0px; border: 0px;"><span style="max-width: 100%; padding: 0px 5px 0px 0px;"><a add_param="link_type=260" style="max-width: 100%; text-decoration: underline; color: #075db3;"><img alt="复制代码" src="http://cdn12.sm-img5.com/?src=l4uLj8XQ0JyQkpKQkdGckZ2TkJiM0ZyQktCWkp6YmozQnJCPhpyQm5rRmJaZ&amp;restype=3&amp;from=derive&amp;pi=&amp;v=1" style="max-width: 100%; border: 0px; text-decoration: none;" /></a></span></div></div><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;">&nbsp;</p><p style="max-width: 100%; padding: 0px; margin: 0px; border: 0px;">第二个JSONP开始初始化。没错&nbsp; responseData又被赋为undefine！！！</p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;"><img alt="" src="http://cdn12.sm-img5.com/?src=l4uLj8XQ0JaSnpiajNGckZaLnZOQmNGckJLQnZOQmM3PzsrQyc3OzMrL0M3PzsrPy9DNyM7OzM%2FNz8%2FJx8zKzM7Rj5GY&amp;restype=3&amp;from=derive&amp;pi=&amp;v=1" style="max-width: 100%; border: 0px;" /></p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;">　　</p><p style="max-width: 100%; padding: 0px; margin: 0px; border: 0px;">第一个script的load开始执行，responseData这时判断绝对为undefined，为毛？因为这是闭包，引用最后一个responseData的值。只能进入error了。</p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;"><img alt="" src="http://cdn12.sm-img5.com/?src=l4uLj8XQ0JaSnpiajNGckZaLnZOQmNGckJLQnZOQmM3PzsrQyc3OzMrL0M3PzsrPy9DNyM7OzcfPzsnMz8zKxs3Rj5GY&amp;restype=3&amp;from=derive&amp;pi=&amp;v=1" style="max-width: 100%; border: 0px;" /></p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;">&nbsp;</p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;"><span style="max-width: 100%; font-weight: 700;"><span sm_bf"="" style="max-width: 100%; font-size: 18px;">问题修复</span></span></p><p style="max-width: 100%; padding: 0px; margin-right: 0px; margin-left: 0px; border: 0px;">　　<span style="max-width: 100%; font-weight: 700;">策略</span>：&nbsp;</p><p style="max-width: 100%; padding: 0px; margin: 0px; border: 0px;">　　1， 修改jsonp源码。在执行callback时，将responseData，传入监听函数。诸如function(data){ return function( ...onload... }(responseData)；这个太麻烦，而且还得注意开源协议。</p><p style="max-width: 100%; padding: 0px; margin: 0px; border: 0px;">　　2，规避jsonp的响应。改成这样一种写法。原理是，只用jsonp发请求，然后后台执行window.callback。</p><div sm_sf"="" style="max-width: 100%; padding: 5px; margin: 0px; border: 1px solid #cccccc; background: #f5f5f5;"><div style="max-width: 100%; margin: 5px 0px 0px; border: 0px;"><span style="max-width: 100%; padding: 0px 5px 0px 0px;"><a add_param="link_type=260" style="max-width: 100%; text-decoration: underline; color: #075db3;"><img alt="复制代码" src="http://cdn12.sm-img5.com/?src=l4uLj8XQ0JyQkpKQkdGckZ2TkJiM0ZyQktCWkp6YmozQnJCPhpyQm5rRmJaZ&amp;restype=3&amp;from=derive&amp;pi=&amp;v=1" style="max-width: 100%; border: 0px; text-decoration: none;" /></a></span></div><pre style="max-width: 100%; white-space: pre-wrap; word-wrap: break-word;">window.checkLogin1 =<span style="max-width: 100%;"> function(data){<br style="max-width: 100%;" />        console.info(</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">checkLogin1 success</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%;">);<br style="max-width: 100%;" />        console.info(data);<br style="max-width: 100%;" />    }<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #0000ff;">for</span>(<span style="max-width: 100%; color: #0000ff;">var</span> i = <span style="max-width: 100%; color: #800080;">0</span>;i&lt;<span style="max-width: 100%; color: #800080;">5</span>;i++<span style="max-width: 100%;">){<br style="max-width: 100%;" />        $.ajax({<br style="max-width: 100%;" />            url:</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;"><span style="max-width: 100%; word-break: break-all;">https://m.suning.com/authStatus?callback=checkLogin1&amp;_=1430100870770</span></span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%;">,<br style="max-width: 100%;" />            dataType:</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%; color: #800000;">jsonp</span><span style="max-width: 100%; color: #800000;">'</span><span style="max-width: 100%;">         });     }</span></pre><div style="max-width: 100%; margin: 5px 0px 0px; border: 0px;"><span style="max-width: 100%; padding: 0px 5px 0px 0px;"><a add_param="link_type=260" style="max-width: 100%; text-decoration: underline; color: #075db3;"><img alt="复制代码" src="http://cdn12.sm-img5.com/?src=l4uLj8XQ0JyQkpKQkdGckZ2TkJiM0ZyQktCWkp6YmozQnJCPhpyQm5rRmJaZ&amp;restype=3&amp;from=derive&amp;pi=&amp;v=1" style="max-width: 100%; border: 0px; text-decoration: none;" /></a></span></div></div><p style="max-width: 100%; padding: 0px; margin: 0px; border: 0px;">　　切记不能加 jsonpCallback：&#8216;checkLogin1&#8217;.原因是，jsonp会重写window[checkLogin1].第二次请求将找不到。</p><div sm_sf"="" style="max-width: 100%; padding: 5px; margin: 0px; border: 1px solid #cccccc; background: #f5f5f5;"><div style="max-width: 100%; margin: 5px 0px 0px; border: 0px;"><span style="max-width: 100%; padding: 0px 5px 0px 0px;"><a add_param="link_type=260" style="max-width: 100%; text-decoration: underline; color: #075db3;"><img alt="复制代码" src="http://cdn12.sm-img5.com/?src=l4uLj8XQ0JyQkpKQkdGckZ2TkJiM0ZyQktCWkp6YmozQnJCPhpyQm5rRmJaZ&amp;restype=3&amp;from=derive&amp;pi=&amp;v=1" style="max-width: 100%; border: 0px; text-decoration: none;" /></a></span></div><pre style="max-width: 100%; white-space: pre-wrap; word-wrap: break-word;"><span style="max-width: 100%; color: #008000;">//</span><span style="max-width: 100%; color: #008000;">回调函数设置，给后台执行</span>         window[callbackName] =<span style="max-width: 100%;"> function(){<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">/*</span><span style="max-width: 100%; color: #008000;"><span style="max-width: 100%; word-break: break-all;">  console.info('callbackName arguments ');<br style="max-width: 100%;" />            console.info(arguments[0]);</span></span><span style="max-width: 100%; color: #008000;">*/</span><span style="max-width: 100%;">             responseData </span>=<span style="max-width: 100%;"> arguments<br style="max-width: 100%;" /></span><span style="max-width: 100%; color: #008000;">/*</span><span style="max-width: 100%; color: #008000;"><span style="max-width: 100%; word-break: break-all;">console.info('responseData ');<br style="max-width: 100%;" />            console.info(responseData);</span></span><span style="max-width: 100%; color: #008000;">*/</span><span style="max-width: 100%;">         }</span></pre><div style="max-width: 100%; margin: 5px 0px 0px; border: 0px;"><span style="max-width: 100%; padding: 0px 5px 0px 0px;"><a add_param="link_type=260" style="max-width: 100%; text-decoration: underline; color: #075db3;"><img alt="复制代码" src="http://cdn12.sm-img5.com/?src=l4uLj8XQ0JyQkpKQkdGckZ2TkJiM0ZyQktCWkp6YmozQnJCPhpyQm5rRmJaZ&amp;restype=3&amp;from=derive&amp;pi=&amp;v=1" style="max-width: 100%; border: 0px; text-decoration: none;" /></a></span></div></div></div></div><img src ="http://www.blogjava.net/liudawei/aggbug/430089.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/liudawei/" target="_blank">孤飞燕</a> 2016-04-14 10:52 <a href="http://www.blogjava.net/liudawei/articles/430089.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>