使用JQuery调用Restful服务
这是服务使用JQuery调用Restful服务,jquery,wcf-rest,Jquery,Wcf Rest,这是服务 [WebGet(UriTemplate = "{city}", ResponseFormat=WebMessageFormat.Json)] string FormatAddress(string city); public string FormatAddress(string city) { return city; } 这是客户。 像这样从url调用http://localhost:8210/formataddress/irvine 按预期返回城市名称 像这
[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
方法参数的各种其他调整
您知道我如何访问onSuccess内的消息吗
顺便说一句,所有项目都在同一个VS2008解决方案中。尝试在ajax调用之前添加以下代码
jQuery.support.cors = true;
请编辑您的帖子并使用“代码格式”按钮。您是否检查了回复代码以确定是否正确?(200、304等)。另外,您是否看到请求正文中的内容实际上如您所期望的那样存在?另外,响应的内容类型是什么(检查响应的标题)您是否确定contentType
不应该是application/x-www-form-urlencoded
forGET
请求?