JS $ .get成功而不是$ .ajax

时间:2016-01-21 12:46:51

标签: javascript jquery

我试图整理一些我的JS代码,并希望更改我的$ .ajax 使用成功函数$ .get

function getresults(){

var reqid = getUrlVars()["id"];
console.log(reqid);
        $.ajax({
        type: "POST",
        url: "/api/ser/id/",
        data: {
            'search_id' : reqid
        },
        success: resultsSuccess,
        dataType: 'json'    
        })

而是再调用另一个函数我可以把它放到这个$ .get中吗? 我知道它不正确,但我得到一个TypeError:回调是目前未定义的任何帮助完成/修复功能将是伟大的。

function getresults(){

    var res_id = getUrlVars()["id"];
        $.get("/api/ser/id/"+res_id+"/")
            .done(function(data){ 
                $.each(data); function ResultsSuccess(){

                    console.log('ResultsSuccess:='+this.pk+'Answer')
                    $('#result_header').text("Information on "+this.preferred_name.capitalizeFirstLetter()+", "+this.last_name.capitalizeFirstLetter());
                    $("#last_name").val(this.last_name.capitalizeFirstLetter());
                    $("#preferred_name").val(this.preferred_name.capitalizeFirstLetter());
                    $("#title").val(this.title);
                    $("#workphone").val(this.workphone);
                    $("#mobile_phone").val(this.mobile_phone);
                    $("#email").val(this.email);
                    $("#notes").val(this.notes);
                    $("#updated_by").val(this.updated_by);
                    $("#updated_at").val(this.updated_at);
                    $("#employstatus").val(this.employstatus);
                    $("#employer").val(this.employer);
                    $("#role").val(this.role_name);
                    $("#location").val(this.location_name);
                }
            })
}

1 个答案:

答案 0 :(得分:1)

你的语法有点偏。您应该使用$.each(data, function(responseData) {,然后使用this替换函数中的responseData

$.each(data, function(responseData) {
    console.log('ResultsSuccess:=' + responseData.pk + 'Answer')
    $('#result_header').text("Information on " + responseData.preferred_name.capitalizeFirstLetter() + ", " + responseData.last_name.capitalizeFirstLetter());
    $("#last_name").val(responseData.last_name.capitalizeFirstLetter());
    $("#preferred_name").val(responseData.preferred_name.capitalizeFirstLetter());
    $("#title").val(responseData.title);
    $("#workphone").val(responseData.workphone);
    $("#mobile_phone").val(responseData.mobile_phone);
    $("#email").val(responseData.email);
    $("#notes").val(responseData.notes);
    $("#updated_by").val(responseData.updated_by);
    $("#updated_at").val(responseData.updated_at);
    $("#employstatus").val(responseData.employstatus);
    $("#employer").val(responseData.employer);
    $("#role").val(responseData.role_name);
    $("#location").val(responseData.location_name);
}