Ajax跨域问题及解决方案(jsonp,cors)

è·¨å

è·¨åæä¸ä¸ªæ¡ä»¶ï¼æ»¡è¶³ä»»ä½ä¸ä¸ªæ¡ä»¶å°±æ¯è·¨å

 1ï¼æå¡å¨ç«¯å£ä¸ä¸è´
 2ï¼åè®®ä¸ä¸è´
 3ï¼ååä¸ä¸è´

解å³æ¹æ¡ï¼

1.jsonp

ããå¨è¿ç¨æå¡å¨ä¸è®¾æ³å¨æçææ°æ®è£è¿jsæ ¼å¼çææ¬ä»£ç æ®µä¸­ï¼ä¾å®¢æ·ç«¯è°ç¨åè¿ä¸æ­¥å¤çï¼å¨åå°éè¿å¨ææ·»å scriptæ ç­¾åsrcå±æ§ï¼è¡¨é¢çä¸å»ä¸ajaxæ为ç¸ä¼¼ï¼ä½æ¯ï¼è¿åajax并没æä»»ä½å³ç³»ï¼ä¸ºäºä¾¿äºä½¿ç¨å交æµï¼éæ¸å½¢æäºä¸ä¸­éæ­£å¼ä¼ è¾åè®®ï¼äººä»¬æå®ç§°ä½ jsonp ã

代ç å¦ä¸ï¼

htmlï¼

<body>
  <form action="/" method="post" enctype="multipart/form-data">
    <input type="text" name="xinxi" id="info"><br>
    <input type="file" name="file" id="file"><br>
    <input type="button" value="æ交" name="submit" id="btn">
  </form>
</body>
<script src="./jquery.js"></script>
<script>
  //æåå好å½æ°ï¼è°ç¨å½æ°éè¦ä¼ å
  function callback(data){
    alert(data);
  }
  //å¨ææ·»å scriptæ ç­¾åsrcå±æ§
  $('#btn').on('click',function(){
    var sc = document.createElement('script');
    sc.src = 'http://soul:8888/kuayu?cb=callback';
    $('head').append(sc); 
  })
</script>ãã

jsï¼

var http = require('http');
 var url = require('url');
 var server = http.createServer();
 server.listen('8888',function(){
   console.log('8888');
 });
 server.on('request',function(req,res){
   var urls = url.parse(req.url,true);
   if(urls.pathname == '/kuayu'){
     res.end('callback("jsonp")');//è¿åçæ°æ®éæ¯å端å®ä¹çå½æ°è°ç¨çå½¢å¼
   }
 });

 è¿è¡ç»æï¼

Ajax跨域问题及解决方案(jsonp,cors)

 æ»ç»ä¸ä¸ï¼

ããjsonpçä¸ä¸ªè¦ç¹å°±æ¯å许ç¨æ·ä¼ éä¸ä¸ªcallbackåæ°ç»æå¡ç«¯ï¼ ç¶åæå¡ç«¯è¿åæ°æ®æ¶ä¼å°è¿ä¸ªcallbackåæ°ä½ä¸ºå½æ°åæ¥å裹ä½JSONæ°æ®ï¼ è¿æ ·å®¢æ·ç«¯å°±å¯ä»¥éæå®å¶èªå·±çå½æ°æ¥èªå¨å¤çè¿åæ°æ®äºã

ããåç°å¡æ¯æ¥æ"src"è¿ä¸ªå±æ§çæ ç­¾é½æ¥æè·¨åçè½åï¼æ¯å¦scriptãimgãiframeï¼ src çè½åå°±æ¯æè¿ç¨çæ°æ®èµæºå è½½å°æ¬å°(å¾çãJS代ç ç­);

2.cors

corsè·¨åçæ ¸å¿ç¹æ¯å¨æå¡ç«¯ä»£ç ä¸­è®¾ç½®ä¸ä¸ªååºå¤´å³å¯

res.setHeader('Access-Control-Allow-Origin','*');

 htmlï¼

<body>
  <form action="/" method="post" enctype="multipart/form-data" id="form">
    <input type="text" name="xinxi" id="info"><br>
    <input type="button" value="æ交" name="submit" id="btn">
  </form>
</body>
<script src="./jquery.js"></script>
<script>
  $('#btn').on('click', function () {      
        $.ajax({
            url: 'http://soul:8888/kuayu',
            type:'delete',  
            async:false,
            success: function (data) {
              alert(data);
            },
        })
  })
</script>

js代ç ï¼

<body>
  <form action="/" method="post" enctype="multipart/form-data" id="form">
    <input type="text" name="xinxi" id="info"><br>
    <input type="button" value="æ交" name="submit" id="btn">
  </form>
</body>
<script src="./jquery.js"></script>
<script>
  $('#btn').on('click', function () {      
        $.ajax({
            url: 'http://soul:8888/kuayu',
            type:'delete',  
            async:false,
            success: function (data) {
              alert(data);
            },
        })
  })
</script>

ææï¼

Ajax跨域问题及解决方案(jsonp,cors)

ãããå¾å¤äººä¹è®¤ä¸ºä½¿ç¨CORS解å³è·¨åå¾ç®åï¼åªéè¦å¨æå¡å¨æ·»å ååºå¤´ â Access-Control-Allow-Origin ï¼* â å°±å¯ä»¥äºï¼

å¶å®ä¸ç¶ï¼å ä¸ºå¨CORS中ï¼ææçè·¨å请æ±è¢«å为äºä¸¤ç§ç±»åï¼ä¸ç§æ¯ç®å请æ±ï¼ä¸ç§æ¯å¤æè¯·æ± (严格æ¥è¯´åºè¯¥å«âéé¢æ£è¯·æ±')ï¼ç®å请æ±ä¸æ®éçajax请æ±æ å¼ï¼ä½å¤æ请æ±ï¼å¿é¡»å¨æ­£å¼åé请æ±åååéä¸ä¸ªOPTIONSæ¹æ³ç请æ±å·²å¾å°æå¡å¨çåæï¼è¥æ²¡æå¾å°æå¡å¨çåæï¼æµè§å¨ä¸ä¼åéæ­£å¼è¯·æ±ï¼

满足以ä¸æææ¡ä»¶ï¼è¢«è§ä¸ºç®åç±»åç请æ±ï¼

1ï¼è¯·æ±æ¹æ³å¿é¡»æ¯ GETãHEADãPOST中çä¸ç§ï¼å¶ä»æ¹æ³ä¸è¡ï¼

2ï¼è¯·æ±å¤´ç±»ååªè½æ¯ AcceptãAccept-LanguageãContent-LanguageãContent-Typeï¼æ·»å å¶ä»é¢å¤è¯·æ±å¤´ä¸è¡ï¼

3ï¼è¯·æ±å¤´ Content-Type å¦ææï¼å¼åªè½æ¯ text/plainãmultipart/form-dataãapplication/x-www-form-urlencoded 中çä¸ç§ï¼å¶ä»å¼ä¸è¡ï¼

4ï¼è¯·æ±ä¸­çä»»æ XMLHttpRequestUpload  对象å没æ注åä»»ä½äºä»¶çå¬å¨ï¼

5ï¼è¯·æ±ä¸­æ²¡æä½¿ç¨ ReadableStream 对象ãï¼ä»¥ä¸æèªè¥¿å²­è湿微信å¬ä¼å·ï¼

æ»ç»ä¸ä¸ï¼

å¦æ请æ±æ¹å¼ä¸ºgetåpostç®å请æ±ï¼ååªéè¦è®¾ç½®ååºå¤´ï¼res.setHeader('Access-Control-Allow-Origin','*');æ¥å许æä¸ä¸ªå æè ææåè¿è¡æ°æ®å±äº«ï¼

è¥æ¯å¶ä»æ¹å¼ç请æ±ï¼ä¼å¨åéçæ­£ç请æ±ä¹ååéä¸ä¸ªoptions请æ±ï¼éè¿options请æ±é设置ï¼res.setHeader('Access-Control-Allow-Methods', 'DELETE')ï¼

åç¥æå¡å¨æ­£å¼è¯·æ±ä¼ä½¿ç¨åªä¸ç§ HTTP 请æ±æ¹æ³ã

æ»ç»

以ä¸æè¿°æ¯å°ç¼ç»å¤§å®¶ä»ç»çAjaxè·¨åé®é¢å解å³æ¹æ¡ï¼jsonpï¼corsï¼,å¸æ对大家ææ帮å©ï¼å¦æ大家æä»»ä½çé®è¯·ç»æçè¨ï¼å°ç¼ä¼åæ¶åå¤å¤§å®¶çãå¨æ­¤ä¹é常æ谢大家对èæ¬ä¹å®¶ç½ç«çæ¯æï¼
å¦æä½ è§å¾æ¬æ对你æ帮å©ï¼æ¬¢è¿è½¬è½½ï¼ç¦è¯·æ³¨æåºå¤ï¼è°¢è°¢ï¼

相关推荐