在phonegap /本地jquery或sencha上向远程服务器发出Ajax请求

发布于 2024-12-26 07:15:07 字数 369 浏览 0 评论 0原文

Mobile Device(file://..)                      Server

index.html                     <--JSON---   post.php
main.js
sencha_.js or Jquery_.js

情况就像上面这样, 所有文件(js、html)均由 PhoneGap 存储在移动设备上。(file://.../www/index.html...) 它将通过WebView显示。

main.js 会调用 $.ajax() 或 Ext.Ajax.request() 从服务器接收 json 数据。 (无论 POST/GET)

有可能吗?是否存在跨域问题?

Mobile Device(file://..)                      Server

index.html                     <--JSON---   post.php
main.js
sencha_.js or Jquery_.js

The situation is like above,
All files (js,html) is on the mobile device by PhoneGap.(file://.../www/index.html...)
It will be showed by WebView.

and main.js will call $.ajax() or Ext.Ajax.request() to receive json data from server. (whatever POST/GET)

is it possible? is there cross-domain problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫腻 2025-01-02 07:15:07

是的,这很有可能。由于您的 PhoneGap 应用程序是从 file:// 协议运行的,因此同源策略不适用,即不存在跨域问题。

Yes, it is very possible. Since your PhoneGap application is running from the file:// protocol the same origin policy does not apply, that is, no cross domain issues.

听你说爱我 2025-01-02 07:15:07

Phonegap 允许向任何服务器发出请求,您不需要使用任何 hacky 解决方案,它就可以工作,只要您在 AndroidManifest.xml 中有以下行

<uses-permission android:name="android.permission.INTERNET" />   

Phonegap allows to do requests to any server, you don't need to use any hacky solutions, it just works, as long as you have the following line in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />   
尽揽少女心 2025-01-02 07:15:07

是的,您可以,但不能使用本机 AJAX 方法。

建立跨浏览器连接的方法之一是创建一个 script 标记并通过回调方法解析所有参数:仅 GET

var url = "http://myPage.com/service.php?parem1=abc&parem2=def&callback=MY_CALLBACK";

window.MY_CALLBACK = function( response ) {
    // Here response have all your data.
}

var script = document.createElement("script");
script.type = "text/javascript";
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);

http:// myPage.com/service.php

<?
    Doing a lot of server site code...

    echo $_GET['callback'] . "(" . $dataToPass . ");";
?>

$_GET["callback"]MY_CALLBACK ,它是我们在全局范围内的 javascript 方法。

希望你能明白。

如果您使用的是框架,我认为几乎所有框架都有一种方法可以做到这一点。

jQuery

var url = "http://myPage.com/service.php?parem1=abc&parem2=def";
$.ajax({
    "url": url,
    "dataType": "jsonp",
    "success": function(response) {
        // Here response have all your data.
    }
});

但你没有错误处理程序:(

你使用插件 jQuery。 jsonp 获取错误处理程序:来自:http://code.google.com/p/jquery-jsonp/wiki/TipsAndTricks

$.jsonp({
  "url": "http://gdata.youtube.com/feeds/api/users/"+userId+"?callback=?",
  "data": {
      "alt": "json-in-script"
  },
  "success": function(userProfile) {
      // handle user profile here 
  },
  "error": function(d,msg) {
      alert("Could not find user "+userId);
  }
});

Yes you can but not using native AJAX methods.

One of the ways to make cross browser connections is to make a script tag and parsing all parameters to it via with a callback method: GET only

var url = "http://myPage.com/service.php?parem1=abc&parem2=def&callback=MY_CALLBACK";

window.MY_CALLBACK = function( response ) {
    // Here response have all your data.
}

var script = document.createElement("script");
script.type = "text/javascript";
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);

http://myPage.com/service.php

<?
    Doing a lot of server site code...

    echo $_GET['callback'] . "(" . $dataToPass . ");";
?>

$_GET["callback"] is MY_CALLBACK witch is our javascript method in the global scope.

Hope you can get the idea.

If you are using a framework i think almost all of then have a method to do this.

jQuery

var url = "http://myPage.com/service.php?parem1=abc&parem2=def";
$.ajax({
    "url": url,
    "dataType": "jsonp",
    "success": function(response) {
        // Here response have all your data.
    }
});

but you have no error handler :(

You use the plugin jQuery.jsonp to get an error handler: From: http://code.google.com/p/jquery-jsonp/wiki/TipsAndTricks

$.jsonp({
  "url": "http://gdata.youtube.com/feeds/api/users/"+userId+"?callback=?",
  "data": {
      "alt": "json-in-script"
  },
  "success": function(userProfile) {
      // handle user profile here 
  },
  "error": function(d,msg) {
      alert("Could not find user "+userId);
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文