0

这里是服务。

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

这是客户。像这样从 url 调用http://localhost:8210/formataddress/irvine 返回城市名称,正如预期的那样。

像这样从 JQuery 调用不会返回成功。

$.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");
}

我试过像这样传递城市名称 data:以及对方法参数{"city" : "irvine"}的各种其他调整。$.ajax

知道如何访问inside onSuccess要显示的消息吗?

顺便说一句,所有项目都在同一个 VS2008 解决方案中。

4

1 回答 1

1

尝试在 ajax 调用之前添加以下代码。

jQuery.support.cors = true;
于 2012-07-18T08:59:18.317 回答