【发布时间】:2013-03-23 01:19:11
【问题描述】:
我有 WCF 服务:
[ServiceContract]
public interface IMunicipiosService
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "ListaMunicipios")]
List<ClsListaMunicipios> GetListaMunicipios();
}
它在chrome中返回json(是JSON还是JSONP?):
{"GetListaMunicipiosResult":[{"MunicipioID":"1","MunicipioNome":"Florianopolis","MunicipioUf":"SC"},{"MunicipioID":"2","MunicipioNome":"Joinville","MunicipioUf":"SC"}]}
我的 JS:
$.ajax("http://localhost:56976/MunicipiosService.svc/ListaMunicipios", {
beforeSend: function (xhr) {
// $.mobile.showPageLoadingMsg();
alert('beforeSend');
},
complete: function () {
// $.mobile.hidePageLoadingMsg();
alert('complete');
},
contentType: 'application/json; charset=utf-8',
dataType: 'jsonp',
type: 'GET',
data: {},
error: function (xhr, ajaxOptions, thrownError) {
alert('not ok 1 ' + xhr.status);
alert('not ok 2 ' + xhr.responseText);
alert('not ok 3 ' + thrownError);
},
success: function (data) {
alert('success');
}
});
但我得到了错误:
不行 1200
不好 2 未定义
不正常 3 错误 jQueryXXXXXXXXX 未被调用
【问题讨论】:
-
除非您正在执行跨域 ajax 请求,否则您不应指定 jsonp。您应该将
dataType选项更新为json -
@awbergs,我将 dataType: 'jsonp' 更改为 dataType: 'json' 现在我得到: 不行 1 0 不行 2 不行 3
-
当我在 index.html 中调用 $.ajax(... 时工作正常。我不知道为什么调用在 .js 文件中不起作用。
-
您是否将 JS 文件托管在“localhost”域上?此外,由于您没有发送任何数据,因此没有任何理由在 ajax 选项中设置 contentType。另外,如果您可以发布可能有帮助的服务方法 impl。
标签: jquery .net json wcf cordova