zoukankan      html  css  js  c++  java
  • XDK html development --- Cross Domain Request

        Two days ago, I came across a problem. After building a restful web service for my app. I happily tried to access the data from the web service using phone. I sent request for data from server using following code:

    $.ajax({
        url: 'http://192.168.1.102:808/quser',
        type: 'GET',
        dataType: 'json',
    	async :false,
        error : function (xhr, ajaxOptions, thrownError) {
    	    alert(xhr.readyState);
            alert(xhr.status);
            alert(thrownError);
          }, 
        success: function (data) {
            alert(data);
        }
    });
    }
    

      I used Intel XDK environment. emulator can return data. But when I test with real phone(both iphone and android) , xhr.status returned value 0. I started to search why this happened. Finally, I was aware of this cross domain request problem. Then start to search 'phone gap cross domain request'. Overall, they give two solutions:

    • JSON response header has Access-Control-Allow-Origin set to allow access to the domain: this is not very common since it poses a security risk. More information here.
    • JSON supports padding (JSONP): This is more commonly used by APIs to allow developers to access their data.

        I tried both, but none of them work. 

        I started feeling bad and disappointed. I went through a hard time :( , But this time, I didn't give up. Kept searching, kept believing that every problem in engineering must get a corresponding solution for it. Answer is here:http://software.intel.com/en-us/html5/articles/how-to-access-JSON-data-in-HTML5-apps.

    Adding following two tags in our file. 

    <script src="intelxdk.js"></script>
    <script src="xhr.js"></script>

    intelxdk.js
     and xhr.js actual files are not required,it will be included automatically in Intel XDK tools, just the script tags should be included.
    Then I use the statement suggested from this site:
    $.getJSON("http://time.jsontest.com", function(data){
        alert(data);
    });
    

     Amazingly, it did work!!!!!Cheers!!!!!

    PS: XDK is really easy to use for beginners.

  • 相关阅读:
    静态库中的静态变量连接失败的解决
    (转)Virtual PC 2007虚拟网络设置
    (zt)iPhone Developer注册
    (zt)正则表达式30分钟入门教程
    (zt)Flex SDK 3.2和Flex Builder 3.0.2升级
    (转)完成端口之性能优化
    (摘)法拉利年代型号
    安装windows下的NDS开发环境
    [C#]小问题
    javascript "未结束字符串"
  • 原文地址:https://www.cnblogs.com/songwanzi/p/3565806.html
Copyright © 2011-2022 走看看