【问题标题】:Calling Restful Service using JQuery使用 JQuery 调用 Restful 服务
【发布时间】:2011-09-16 13:03:39
【问题描述】:

这里是服务。

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

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

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

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

【问题讨论】:

  • 请编辑您的帖子并使用代码格式化按钮。
  • 您检查响应码是否正常? (200、304 等)。您是否也看到请求正文中的内容实际上如您所愿?
  • 另外,响应的内容类型是什么(检查响应的标头)
  • 你确定contentType 不应该是application/x-www-form-urlencoded 用于GET 请求吗?

标签: jquery wcf-rest


【解决方案1】:

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

jQuery.support.cors = true;

【讨论】:

    猜你喜欢
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    相关资源
    最近更新 更多