我正在为/ PhoneGap创建一个移动应用程序。我试图通过自己创建的ASP.NET Web服务加载外部数据。当我使用Ripple Emulator运行本地并且将url设置为本地Web Service时,Web Service正常工作。我将url更改为在线版本的时刻,所以跨平台,它不再在我的本地Ripple仿真器中,我得到以下错误:
SyntaxError: Unexpected token e
at Object.parse (native)
at IncomingMessage.module.exports (/app/node_modules/express/node_modules/connect/lib/middleware/json.js:76:27)
at IncomingMessage.EventEmitter.emit (events.js:93:17)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socket.ondata (http.js:1825:22)
at TCP.onread (net.js:404:27)"
我在线阅读了很多可能的解决方案,但似乎无法解决问题。阅读Phonegap, jQueryMobile And Web service后,我认为它很简单,如果没有JSONP,Proxies,HttpHandlers等解决方案,它应该可以正常工作。
下面我添加了一些应该相关的代码:
HTML / JAVASCRIPT
function onBodyLoad() {
// Add the PhoneGap deviceready event listener
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
// Test the function
login();
}
function login() {
try {
// Validate credentials
$.ajax({
type: "POST",
url: 'http://login.multiviewer.nl/Mobile.asmx/ValidateCredentials',
contentType: "application/json; charset=utf-8",
data: '{ username: "x", password: "y", browser: "", title: "", ipAddress: "" }',
dataType: "json",
success: function (object) {
alert("success");
},
error: function (em, msg) {
alert("function error");
}
});
}
catch (err) {
alert("catch error");
}
}
//-->
</script>
ASP.NET Web服务ASMX
namespace Web.Core.WebServices
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Mobile : WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string ValidateCredentials(string username, string password, string browser, string title, string ipAddress)
{
Dictionary<string, string> keyValues = new Dictionary<string, string>();
keyValues.Add("Something", "Ok");
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Serialize(keyValues);
}
}
}
的PhoneGap
<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" id="com.multiviewer.app" version="1.0.1" versionCode="1">
<!-- General app settings -->
<name>App name</name>
<description>App description</description>
<author href="http://www.rhainesoft.nl/" email="info@rhainesoft.nl">RhaineSoft</author>
<!-- App icon -->
<icon src="images/icon.jpg" />
<!-- Features used, rights needed at installing -->
<preference name="permissions" value="none"/>
<preference name="orientation" value="default" />
<preference name="target-device" value="universal" />
<!-- Expose access to all URIs, including the file and http protocols -->
<access origin=".*"/>
<!-- Content -->
<content src="index.html" />
</widget>
我很感激帮助解决这个问题。如果您有任何疑问或错过了什么,请告诉我。