2

我正在为/使用 PhoneGap 创建一个移动应用程序。我正在尝试通过我自己创建的 ASP.NET Web 服务加载外部数据。当我使用 Ripple Emulator 运行本地并将 url 设置为本地 Web 服务时,Web 服务工作正常。当我将 url 更改为在线版本时,跨平台,它在我的本地 Ripple Emulator 中不再适用,我收到以下错误:

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 和Web 服务之后,我认为这很简单,它应该可以在没有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);
        }
    }
}

电话间隙

<?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>

我很感激任何帮助我解决这个问题。如果您有任何问题或我错过了什么,请告诉我。

4

0 回答 0