ajax和getJSON调用之间的区别

Difference between ajax and getJSON calls?

本文关键字:区别 之间 调用 getJSON ajax      更新时间:2023-09-26

我有一个页面由位于http://server/application的Web.API应用程序提供。在客户端,我正在执行GET以从服务器中提取一些数据。问题是我认为应该起作用的东西不起作用。

此代码有效:

$.ajax( {
    url: "api/slideid/getdirectories/",
    dataType: 'json',
    success: function ( data ) {
        setPaths( data );
    }
} );

但事实并非如此:

$.getJSON( "api/slideid/getdirectories/",
    function ( data ) {
        setPaths( data );
    } );

在fiddler中看到的第一个例子中,它试图从中检索数据的url是http://server/application/api/slideid/getdirectories,这是正确的。

第二种是http://server/api/slideid/getdirectories,这是不对的。我认为json GET的这两种方法是相同的。。。。但他们似乎不是?

有趣的是,这两种方法都适用于我的本地开发盒——只有在我的暂存服务器上,一种方法有效,另一种方法无效。据我所知,IIS设置是相同的,而且我很好地进行了检查。

所以我想知道,当jQuery文档声明getJSON只是.ajax调用的简写时,为什么getJSON不起作用?

编辑:我已经放入了一个显式版本的getJSON,希望显示它们是非常相似的调用,但"真正的"getJSON调用现在已经存在了。

您有一个错误的$.getJSON()实现。这应该是:

$.getJSON(url, {data:data}, function(data){
     alert(data);
});

其中CCD_ 5是可选的。

从文档中:

这是一个简写的Ajax函数,相当于:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});
  $.getJSON(url, {data:data}, ....

错误语法