larrymol
larrymol

Reputation: 101

Calling Restful Service using JQuery

Here's the service.

   [WebGet(UriTemplate = "{city}", ResponseFormat=WebMessageFormat.Json)]
   string FormatAddress(string city);
   public string FormatAddress(string city) {  return city; }

Here's the client. Calling from the url like so http://localhost:8210/formataddress/irvine returns the city name, as expected.

Calling from JQuery like so doesn't return success.

$.ajax({
        type: "GET",
        url: "http://localhost:8210/formataddress/irvine",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: onError,
        success: onSuccess
    });

function onSuccess(data, status) {
    alert("inside onSuccess");
}

function onError(data, status) {
    alert("inside onError");
}

I've tried passing the city name like so data: {"city" : "irvine"} and various other tweaks to the $.ajax method parameters.

Any idea's how I can access the inside onSuccess message to display ?

BTW all the projects are in the same VS2008 solution.

Upvotes: 0

Views: 986

Answers (1)

edwardl
edwardl

Reputation: 13

Try adding the following code before the ajax call.

jQuery.support.cors = true;

Upvotes: 1

Related Questions