你好,我一直在尝试从 PhoneGap-Android 应用程序使用 ASP.NET 编写的 Web 服务,但我似乎在某个地方犯了错误。
值得一提的是,当我从 Eclipse 在 Android 模拟器上运行它时,它会失败。我已经在网络浏览器中尝试过相同的代码,它工作得很好。
这是以下部分索引.html这与问题相关
/* Here i declare 'webServiceURL' which holds the path to the service that's
hosted at 10.0.0.174 (WLAN ip of my computer). I use this instead of 127.0.0.1
because on a mobile phone localhost points to the phone itself. */
// Here i declare 'datos' which are the parameters sent to the web service method
$.ajax({
url: webServiceURL + "InicioSesion",
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(datos),
dataType: 'json',
beforeSend: function() {$.mobile.loading('show')},
complete: function() {$.mobile.loading('hide')},
success: function (data, textStatus, jqXHR) {
// Here i do stuff with 'data'
},
error: function (jqXHR, textStatus, errorThrown) {
// Here i print errors
},
)};
访问原始权限添加到phonegap配置文件
<access origin="*"/>
修改网络配置ASP.NET Web 服务的
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
我面临的错误是,当我将“dataType”设置为“json”(这是我所期望的)时,ajax 请求失败并打印“textStatus”给我解析器错误.
因此,我尝试使用“dataType”作为“text”而不是“json”来查看 Web 服务响应是否有问题,我意识到问题在于响应为 NULL。
请记住我提到的,此代码在 Web 浏览器上完美运行,但在从 Android 模拟器运行的 PhoneGap 应用程序上失败。
如果任何人有一点使用phonegap使用ASP.NET Web服务的经验可以帮助我!我不知道我错过了什么或做错了什么!我已经为此工作了两天,但找不到解决方案!