我有一个Phonegap / Jquery移动应用程序,我试图为Android包装,但我使用的目标SDK不允许Ajax /跨域请求。 简而言之,这些请求适用于最新的Android 4.0.3(API 15),但不适用于Android 2.3.3(API 10),这是我需要与Galaxy Tablet兼容的。 我很难调试这个,因为我无法看到实际的错误,因为我被包装在phonegap和用户Firebug / Chrome网络工具中。我在developer.android.com上没有任何运气。 我已经尝试了位于http://jquerymobile.com/test/docs/pages/phonegap.html的步骤,包括 $ .mobile.allowCrossDomainPages = true; $ .support.cors = true。
这也不起作用。 任何人都可以帮助我,我不知道还有什么可以尝试。 谢谢!
以下是代码的快速示例。请注意,它返回success = true但是命中'Response is not a XML element'错误:
return $.soapRequest({
url: url,
namespace: 'testns',
returnJson: false,
shortMethod: 'methodname',
method: 'longmethodname',
params: params || {},
success: function(data) {
if (data && data.documentElement) {
// hits this on Android SDK 15
if (successFn) {
return successFn(data.documentElement);
}
}
else {
// hits this on Android SDK 10
return fail("Response is not an XML element!");
}
},
error: function(str) {
return fail(str);
}
答案 0 :(得分:0)
尝试使用phonegap插件编写httpConnection。它就是CrossDomain
httpPlugin.java
package com.android.test;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
import com.android.connection.HTTPConnect;
public class HttpPlugin extends Plugin {
public final String ACTION_SEND_MESSAGE = "SendCommand";
private HTTPConnect httpConnect;
public HttpPlugin() {
// TODO Auto-generated constructor stub
httpConnect = new HTTPConnect();
}
@Override
public PluginResult execute(String action, JSONArray arg1, String callbackId) {
PluginResult result = new PluginResult(Status.INVALID_ACTION);
if (action.equals(ACTION_SEND_MESSAGE)) {
try {
String message = arg1.getString(0);
String receiveString = httpConnect.setURL(message);
if(receiveString == null){
//show error result
result = new PluginResult(Status.ERROR,"kakaka");
}else{
Log.v("MAN", "data received");
result = new PluginResult(Status.OK);
}
result = new PluginResult(Status.OK);
} catch (JSONException ex) {
// TODO Auto-generated catch block
result = new PluginResult(Status.JSON_EXCEPTION, ex.getMessage());
}
}
return result;
}
}
plugin.xml文件
Httpplugin .js
var Httpplugin = function () {};
Httpplugin.prototype.post = function (message, successCallback, failureCallback) {
// navigator.notification.alert("OMG");
return cordova.exec(successCallback, failureCallback, 'Httpplugin', 'SendCommand', [message]);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("http", new Httpplugin());
});