jq-跨域请求

在使用的时候,常用的是跨域请求:

跨域问题解决方案:

 1.使用script标签,及json数据需要赋值给变量 
  e.g :  
  e.g : var json={"Id":2,"name":"豆角炒肉"};



 2.使用ajax请求
   jquery 的跨域请求
   
   注意: 1) 设置datatype为jsonp ,jsonp 为callback,其次为callback设置返回函数
          2)这里的callback函数名是已经存在的解析函数,例如go,这个存在,
            则下面的success方法不会执行。
 3) 最后是json数据封装的时候,必须用函数名称来封装,例如下面列子中



  
 e.g : 


  json数据封装格式:go({"Id":2,"name":"豆角炒肉"});


 function go(json){
  console.log(json.length);
  for(var j=0;j



3.第三种方式是 动态的添加 script 标签。
  
e.g :封装创建script的函数
  function getJson(url){
   var script=document.createElement('script');
   script.type='text/javascript';
   script.src=url;
   document.head.appendChild(script);
 }


  调用:1)直接调用,调用后 延迟解析,因为创建后需要加载(不推荐),

         json数据封装为第一种格式; e.g :

     getJson('http://169.254.4.210/dish.json');
            setTimeout(function(){
              for(var j=0;j


       
         2)直接调用 ,在url里添加callback函数名,使用方便,需要声明
 解析函数,数据封装格式为第2种方式

	    function go(json){
                  console.log(json.length);
                  for(var j=0;j




你可能感兴趣的:(phonegap,javascript)