0

So, I am very new to creating web services, but have successfully managed to make a simple webservice which returns information as I'd need from a database as List(Of dictionary(of string, string)) object.

For the purpose of testing, I have created this manually, my code looks like this:

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Serialization

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class test
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function dic() As String
    Dim newDic As New List(Of Dictionary(Of String, String))
    Dim one As New Dictionary(Of String, String)
    one.Add("id", "1")
    one.Add("name", "the name")
    one.Add("price", "5.99")
    newDic.Add(one)
    Dim two As New Dictionary(Of String, String)
    two.Add("id", "2")
    two.Add("name", "item name two")
    two.Add("price", "1299")
    newDic.Add(two)
    Dim s As New JavaScriptSerializer
    Dim str As String = s.Serialize(newDic)
    Return str
End Function

End Class

This webservice "dic" gives me serialized string/list looking like this:

[{"id":"1","name":"the name","price":"5.99"},{"id":"2","name":"item name two","price":"1299"}]

I can read this in VB code like this:

Sub loadMe() Handles Me.Load
    Dim t As New websvce.testSoapClient
    Dim d As String = t.dic
    Dim s As New JavaScriptSerializer
    Dim d2 = s.DeserializeObject(d)
    Response.Write(d2(1)("name") & "<hr>")
End Sub

which gives me the output of the "name" element with index "1". It works fine.

However, unsurprisingly, it does not work when trying to grab this info with jQuery using code as follows:

    $(document).ready(function () { 
        $.getJSON('URL/test.asmx/dic', function (data) {
            alert(data);
        });
    });

So I have spent a good part of the day Googling this, and found all kinds of comments and conversations telling me about why cross domain scripting is not allowed and that there are ways round it using Proxies, adding headers to pages and so on... but... no conclusive solution to the problem.

Here's some of the SO questions I have found:

Origin http://localhost is not allowed by Access-Control-Allow-Origin

How to implement "Access-Control-Allow-Origin" header in asp.net

There's many more, but I cannot find an absolute solution, which fits my criteria. I want to output data in a format similar to that above, and access it from another domain with jQuery.

The easiest work around I have found is to write a handling page which runs on the receiving server, simply loading the data as per my VB code example above and operating as a webMethod on that server to spit out JSON, but I'd rather the whole thing could be handling by the web service, thus no need to run extra files on the consuming server.

EDIT; actually, I do also need to be able to perform POST operations.

Community
  • 1
  • 1
Jamie Hartnoll
  • 7,231
  • 13
  • 58
  • 97
  • If you're only doing GET calls to retrieve data then using [JSONP](http://www.jquery4u.com/json/jsonp-examples/) might be a possibility. I've found that link had reasonable examples which helped me but [this](http://www.ibm.com/developerworks/library/wa-aj-jsonp1/) link has a bit more in-depth background & converage – nkvu Mar 26 '13 at 18:22
  • Thanks, I'll have a read. Actually, I should add, I do also need to POST. I'll edit my question. – Jamie Hartnoll Mar 26 '13 at 18:28
  • Thanks for clarifying - JSONP will support GET requests but not POST requests. I think if you look around (e.g. Google "JSONP Post") you'll find some workarounds etc but your mileage will probably vary if you choose to use them.... – nkvu Mar 26 '13 at 18:38

0 Answers0