javascript Phonegap 中的 Jquery ajax 调用到 RESTful API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5479043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:19:45  来源:igfitidea点击:

Jquery ajax call within Phonegap to RESTful API

javascriptjqueryandroidrestcordova

提问by Josh

I'm trying to make a RESTful api call from the android emulator (using android 2.2). On my server for the login request I'm setting the cors headerresponse.setHeader("Access-Control-Allow-Origin", "*");

我正在尝试从 android 模拟器(使用 android 2.2)进行 RESTful api 调用。在我的登录请求服务器上,我正在设置cors 标头response.setHeader("Access-Control-Allow-Origin", "*");

This exact code works fine from Firefox 4 and Chrome 10 and I was led to believe the android browser resolves this header from version 2.1 up.

这个确切的代码在 Firefox 4 和 Chrome 10 上运行良好,我相信 android 浏览器从2.1版开始解析这个标头。

usr = $("#email").val();
pwd = $("#password").val();

$.ajax({
    url: "http://myremoteserver/login",
    data: {"username": escape(usr), "password": escape(pwd)},
    dataType: "json",
    headers: {"Accept": "application/json"},
    success: function(response) {
      console.log("Success: " + response);
      if (response.result == "success") {
        //doStuff
      }
      else {
        console.log("Success Error: " + response);
        $("#error").html(response);
      }
    },
    error: function(request, status, error) {
      console.log("Error status " + status);
      console.log("Error request status text: " + request.statusText);
      console.log("Error request status: " + request.status);
      console.log("Error request response text: " + request.responseText);
      console.log("Error response header: " + request.getAllResponseHeaders());
      $("#error").html(status);
    }

});

The server never receives the request and the status code is 0 which I've read can mean a cross scripting error. But, as I've said, it works fine in modern browsers.

服务器永远不会收到请求,状态代码为 0,我读过这可能意味着交叉脚本错误。但是,正如我所说,它在现代浏览器中运行良好。

These are the pertinent logs I see in the LogCat

这些是我在 LogCat 中看到的相关日志

03-29 20:30:46.935: DEBUG/PhoneGapLog(277): file:///android_asset/www/index.html: Line 36 : Error status error
03-29 20:30:46.935: INFO/Web Console(277): Error status error at file:///android_asset/www/index.html:36
03-29 20:30:46.954: DEBUG/PhoneGapLog(277): file:///android_asset/www/index.html: Line 37 : Error request status text: error
03-29 20:30:46.954: INFO/Web Console(277): Error request status text: error at file:///android_asset/www/index.html:37
03-29 20:30:46.985: DEBUG/PhoneGapLog(277): file:///android_asset/www/index.html: Line 38 : Error request status: 0
03-29 20:30:46.985: INFO/Web Console(277): Error request status: 0 at file:///android_asset/www/index.html:38
03-29 20:30:47.003: DEBUG/PhoneGapLog(277): file:///android_asset/www/index.html: Line 39 : Error request response text: 
03-29 20:30:47.003: INFO/Web Console(277): Error request response text:  at file:///android_asset/www/index.html:39
03-29 20:30:47.034: DEBUG/PhoneGapLog(277): file:///android_asset/www/index.html: Line 40 : Error response header: 
03-29 20:30:47.034: INFO/Web Console(277): Error response header:  at file:///android_asset/www/index.html:40
03-29 20:33:38.704: DEBUG/SntpClient(65): request time failed: java.net.SocketException: Address family not supported by protocol

As you can see there isn't a whole lot there... makes it a pain to try to debug anything.

正如您所看到的那样,那里并没有很多......尝试调试任何东西都很痛苦。

Edit: AndroidManifest.xml permissions

编辑:AndroidManifest.xml 权限

  <uses-permission android:name="android.permission.CAMERA" />
  <uses-permission android:name="android.permission.VIBRATE" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
  <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.RECEIVE_SMS" />
  <uses-permission android:name="android.permission.RECORD_AUDIO" />
  <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
  <uses-permission android:name="android.permission.READ_CONTACTS" />
  <uses-permission android:name="android.permission.WRITE_CONTACTS" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

采纳答案by Josh

This was caused by the android emulator not being able to connect to the internet. It was fixed by adding -dns-server X.X.X.X(where X.X.X.X is my dns server) to the "Default emulator options" in the Android preferences in eclipse.

这是由于 android 模拟器无法连接到互联网造成的。通过将-dns-server X.X.X.X(其中 XXXX 是我的 dns 服务器)添加到 eclipse 中的 Android 首选项中的“默认模拟器选项”来修复它。

回答by katsuya

As you mentioned you are calling RESTful API for login service, I think you need to specify the ajax type to be POST method because it is GET method in default. i.e.,

正如您提到的,您正在为登录服务调用 RESTful API,我认为您需要将 ajax 类型指定为 POST 方法,因为它默认为 GET 方法。IE,

$.ajax({ type: 'POST', ... });

$.ajax({ type: 'POST', ... });

I think that is why server is not returning any response.

我认为这就是服务器不返回任何响应的原因。

If this does not fix your problem, could you tell me if you can successfully access other web services (anything on the web) from the application?

如果这不能解决您的问题,您能否告诉我您是否可以从应用程序成功访问其他 Web 服务(Web 上的任何内容)?