参考文章:

浏览器同源政策及其规避方法

跨域资源共享 CORS 详解

 

使用jQuery实现跨域提交表单数据

  1. <form action="http://v.juhe.cn/weather/index">
  2.     <label>输入要查询的城市名称:<input type="text" name="cityname" ></label>
  3.     <button type="submit" >查询</button>
  4. </form>

方法1:

  1. function addScriptTag(src){
  2.     var script=document.createElement("script");
  3.     script.setAttribute("type","text/javascript");
  4.     script.src=src;
  5.     document.body.appendChild(script);
  6. }
  7. function foo(data) {
  8.     console.log(data.result.today);
  9. }
  10. $(document).ready(function(){
  11.     //当发生 submit 事件时运行的函数
  12.     $("form").submit(function(e){
  13.         addScriptTag("http://v.juhe.cn/weather/index?   cityname=%E8%8B%8F%E5%B7%9E&key=47ae6e500980056c1defce105b3a90c3&callback=foo");
  14.     });
  15. })

 

方法2:

参考文章:http://www.helloweba.com/view-blog-166.html

  1. $(document).ready(function(){
  2.     var data=$("form").serialize(); //输出序列化表单值的结果
  3.     data+="&key=47ae6e500980056c1defce105b3a90c3";
  4.     $.getJSON("http://v.juhe.cn/weather/index?callback=?",
  5.         data,
  6.         function (responseText) {
  7.        conlo.log(responseText);
  8.         }
  9.     );
  10. });

 

相关文章:

  • 2022-12-23
  • 2021-11-21
  • 2021-07-15
  • 2021-12-21
  • 2022-12-23
  • 2022-02-12
  • 2021-12-29
猜你喜欢
  • 2022-01-17
  • 2021-07-26
  • 2021-11-28
  • 2021-11-22
  • 2021-06-09
  • 2021-04-15
  • 2021-11-16
相关资源
相似解决方案