2011-11-05 119 views
0

我有以下几点:我可以使用jQuery简化这个ajax调用吗?

$.ajax({ 
    type: "POST", 
    traditional: true, 
    url: "/Administration/Locations/DoAction", 
    data: { partitionKey: id.substring(14), 
      rowKey: id.substring(4, 14), 
      action: ac, 
      datastore: $("#DataSource").val() 
    }, 
    async: false, 
    dataType: "json", 
    timeout: 1000, 
    success: function (data) { 
     xx 
    }, 
    error: function (x, t, m) { 
     xx 
    } 

可以,我可以使用jQuery使后简化这个?请注意,id和ac是较早分配的javascript变量。

+1

简化什么办法吗?这是非常简单的代码,但可能会在其他代码的上下文中进行更改。你能解释一下你的问题吗? – Bojangles

+1

你想简化一下吗?看起来相当原始/简单。 – Shomz

+2

'async:false'击败了ajax的目的。 – AlienWebguy

回答

0
$.ajaxSetup({async:false}); 
$.post("/Administration/Locations/DoAction", 
    { partitionKey: id.substring(14), 
      rowKey: id.substring(4, 14), 
      action: ac, 
      datastore: $("#DataSource").val() 
    }, 
    function (data) { 
     xx 
    }, 
    "json" 
).error(function() { 
    xx 
}); 

文档http://api.jquery.com/jQuery.post/

$.post()是相同

$.ajax({ 
    type: 'POST', 
    url: url, 
    data: data, 
    success: success, 
    dataType: dataType 
}); 
+0

遗漏了异步。 –

+0

看起来不错。我只是看看这个链接,看看我可以如何设置异步:false。在这种情况下我需要这个。 –

+0

请参阅我的新答案 – Peter

0

根据您的应用程序,你可能能够使用一个全局的ErrorHandler和/或successhandler。但最有可能的只有第一个。结合彼得的回答,你应该很好走。要设置这些全球处理程序使用

$.ajaxError(...) 
$.ajaxSuccess(...) 

请参阅http://api.jquery.com/jQuery.ajaxSetup/。列表中列出了所有全局ajaxHandler初始化程序的列表。

0

检查,如果这对你的作品:

$.ajax({ 
type: "POST", 
traditional: !0, 
url: "/Administration/Locations/DoAction", 
data: { 
    partitionKey: id.substring(14), 
    rowKey: id.substring(4, 14), 
    action: ac, 
    datastore: $("#DataSource") 
     .val() 
}, 
async: !1, 
dataType: "json", 
timeout: 1e3, 
success: function() {}, 
error: function() {} 
}) 
相关问题