0

我有一个这样创建的服务。

服务.vb

_ 公共类 UmbrellaService 继承 System.Web.Services.WebService 实现 IUmbrellaMobileService

Function GetCustomers() As List(Of Customers) Implements IUmbrellaMobileService.GetCustomers

    Try
        Dim Cust As List(Of Customers) = New List(Of Customers)

        Dim SQLSTR As String = ""
        SQLSTR = "Select Companies.Code, Companies.Name FROM Companies WHERE ISCustomer = '1'"

        Dim ErrorMessage As String = ""
        ErrorMessage = UmbrellaDataManagementObj.OpenConnection(SQLServer, UmbrellaDatabase, UserCode, UserPassword)
        If ErrorMessage <> "" Then
            System.Diagnostics.EventLog.WriteEntry("Umbrella Mobile Service", ErrorMessage & vbNewLine & UmbrellaDataManagementObj.ConnectionString, EventLogEntryType.Error)
            WebOperationContext.Current.OutgoingResponse.StatusCode = 501
            WebOperationContext.Current.OutgoingResponse.StatusDescription = ErrorMessage


            Return Nothing
        Else
            Dim CustomerDetails As DataSet
            CustomerDetails = UmbrellaDataManagementObj.GetDataSQL(SQLSTR)
            If Not CustomerDetails Is Nothing Then
                CustomerDetails.DataSetName = "Companies"
                CustomerDetails.Tables(0).TableName = "Companies"
                Dim CustomerTable As DataTable
                Dim CustomerRow As DataRow

                If CustomerDetails.Tables.Count > 0 Then
                    CustomerTable = CustomerDetails.Tables(0)
                    If CustomerTable.Rows.Count > 0 Then

                        Dim i As Integer

                        For i = 0 To CustomerTable.Rows.Count - 1
                            CustomerRow = CustomerTable.Rows(i)
                            Dim CC As New Customers
                            CC.Code = CustomerRow.Item("Code")
                            CC.Name = CustomerRow.Item("Name")

                            Cust.Add(CC)


                        Next i
                        ' Serialize the results as JSON
                        Dim serializer As DataContractJsonSerializer = New DataContractJsonSerializer(Cust.GetType())
                        Dim Stream As MemoryStream = New MemoryStream

                        serializer.WriteObject(Stream, Cust)

                        ' Return the results serialized as JSON
                        Dim json As String = Encoding.Default.GetString(Stream.ToArray())
                        '  Return json
                        Return Cust
                        UmbrellaDataManagementObj.CloseConnection()
                        WebOperationContext.Current.OutgoingResponse.StatusCode = 200
                        WebOperationContext.Current.OutgoingResponse.StatusDescription = "OK"
                    End If
                End If



                'Return Cust
            Else
                System.Diagnostics.EventLog.WriteEntry("Umbrella Mobile Service", UmbrellaDataManagementObj.ErrorMessage, EventLogEntryType.Error)

                WebOperationContext.Current.OutgoingResponse.StatusCode = 501
                WebOperationContext.Current.OutgoingResponse.StatusDescription = UmbrellaDataManagementObj.ErrorMessage

                Cust = Nothing
                Return Nothing
            End If
        End If



    Catch ex As Exception
        System.Diagnostics.EventLog.WriteEntry("Umbrella Mobile Service", ex.Message, EventLogEntryType.Error)

        WebOperationContext.Current.OutgoingResponse.StatusCode = 501
        WebOperationContext.Current.OutgoingResponse.StatusDescription = ex.Message


        Return Nothing
    End Try
    Dispose()
End Function

界面如下所示:

<ServiceContract()> _
Public Interface IUmbrellaMobileService


    <OperationContract()> _
       <WebInvoke(Method:="GET", BodyStyle:=WebMessageBodyStyle.Bare, ResponseFormat:=WebMessageFormat.Json)> _
    Function GetCustomers() As List(Of Customers)

End Interface

我的数据合同看起来像:

<DataContract()> _
Public Class Customers
    Dim CompanyName As String
    Dim CompanyCode As String


    <DataMember()> _
    Public Property Name() As String
        Get
            Return CompanyName
        End Get
        Set(ByVal value As String)
            CompanyName = value
        End Set
    End Property

    <DataMember()> _
    Public Property Code() As String
        Get
            Return CompanyCode
        End Get
        Set(ByVal value As String)
            CompanyCode = value
        End Set
    End Property
End Class

现在,当我输入地址http://agilesoft.dyndns.org/UmbrellaMobileService/GetCustomers时,我得到的 JSON 数组返回如下:

[{"Code":"001","Name":"rainbow"},{"Code":"009MAY","Name":"AMG AUDIO : HIRE ACC."}]

我正在尝试在我的 Devextreme 应用程序中显示此信息。这是我的 dxview 的代码:

<div data-options="dxView : { name: 'Customer', title: 'Customer' } " >
    <div  data-options="dxContent : { targetPlaceholder: 'content' } " >

               <div data-bind="dxList: { items: listItems }">
            <div data-options="dxTemplate: { name: 'item' } ">
                <div data-bind="text: Name"></div>
            </div>
        </div>          
        </div>
</div>

这是js文件的代码:

UmbrellaMobile.Customer = function (params) {

    var baseAddress = 'http://agilesoft.dyndns.org/UmbrellaMobileService/GetCustomers';
    var listItems;
    var viewModel = {
        Customers: new DevExpress.data.CustomStore({
            load: function () {
                return $.ajax({
                    url: baseAddress,
                    crossOrigin: true,
                    jsonp: true,
                    type: 'GET',
                    data: '{}',
                    dataType: 'jsonp',
                    contentType: "application/json; charset=utf-8",
                    success: function (res) {
                        listItems: data;
                        console.log("success");
                    },
                    error: function (res) {
                        console.log("error");
                    }
                });
            }
        })

    };

    return {
        listItems: listItems,
        viewModel: viewModel
    };
};

当我在 Firefox 中运行此应用程序时,我收到一条消息“没有要显示的数据”

我试图调试代码。我已经设置了断点并查看了控制台窗口以获取任何提示。我没有收到任何错误。ajax 调用永远不会被执行,或者看起来它永远不会被击中

我只想显示信息

谁能帮忙看看是什么问题?

4

1 回答 1

0

好的,无论如何感谢您的阅读。

原来问题在于我的服务不支持 CORS

于 2015-09-14T11:28:36.353 回答