简体   繁体   English

如何获取对象的特定部分作为字符串

[英]How do I get certain part of object as string

I am trying to get data from a server depends on logged in user's name. 我试图从服务器获取数据取决于登录用户的名称。

I succeed to get correct object, but I failed to get only certain part of it. 我成功地获得了正确的对象,但是我却只获得了一部分。

getDepartmentByEmp : function (){
    var empName = $.trim($(".temp-test").html());
    console.log(empName);
    $.ajax({
        contentType : "application/json",
        dataType : 'json',
        type : "GET",
        url : "<c:url value='/app/general/add/getDepartment/'/>" + empName,
        complete : function(data) {
            $("#docDepartment").val(data.responseText);
            $("#docDepartment").prev().html(data.responseText);
            console.log(data.responseText);
            console.log(typeof data.responseText);
        }
    });
},

That empName gets each logged in users' empNameTrim value in my DB. 该empName获取我数据库中每个已登录用户的empNameTrim值。 The type of data is object and responseText is string. 数据类型为对象,responseText为字符串。

And its output looks like following: 其输出如下所示:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

I want to make the value of docDepartment equals to the value of department which will be SM in this case. 我想使docDepartment的值等于部门的值,在这种情况下,部门的值将是SM。

Thank you in advance. 先感谢您。

EDIT : I followed Loïc Faure-Lacroix's tips, modified my code like following: 编辑 :我遵循了LoïcFaure-Lacroix的提示,如下修改了我的代码:

1st 第一

getDepartmentByEmp : function (){
    var empName = $.trim($(".temp-test").html());
    console.log(empName);
    $.ajax({
        contentType : "application/json",
        dataType : 'json',
        type : "GET",
        url : "<c:url value='/app/general/add/getDepartment/'/>" + empName,
        complete : function(data) {
            var doc = JSON.parse(data.responseText);
            $("#docDepartment").val(doc.department);
            $("#docDepartment").prev().html(doc.department);
            console.log(doc.department);
            console.log(typeof doc.department);
        }
    });
},

2nd 第二名

getDepartmentByEmp : function (){
    var empName = $.trim($(".temp-test").html());
    console.log(empName);
    $.ajax({
        contentType : "application/json",
        dataType : 'json',
        type : "GET",
        url : "<c:url value='/app/general/add/getDepartment/'/>" + empName,
        complete : function(data) {
            $("#docDepartment").val(data.responseJSON.department);
            $("#docDepartment").prev().html(data.responseJSON.department);
            console.log(data.responseJSON.department);
            console.log(typeof data.responseJSON.department);
        }
    });
},

3rd 第三名

getDepartmentByEmp : function (){
    var empName = $.trim($(".temp-test").html());
    console.log(empName);
    $.ajax({
        contentType : "application/json",
        dataType : 'json',
        type : "GET",
        url : "<c:url value='/app/general/add/getDepartment/'/>" + empName,
    })
    .done(function (data) {
        $("#docDepartment").val(data.department);
        $("#docDepartment").prev().html(data.department);
        console.log(data.department);
        console.log(typeof data.department);
    })
},

All of them works fine. 他们都工作正常。 Choose whatever you like. 选择任何你喜欢的。

If jQuery isn't parsing to JSON, use JSON.parse to do it on the responseText... That said, according to the documentation here , if you go to the data types section, you should read the following: 如果jQuery是不是要解析JSON,使用JSON.parse做它的responseText的......这就是说,根据文档在这里 ,如果你去的数据类型部分,你应该阅读以下内容:

If json is specified, the response is parsed using jQuery.parseJSON before being passed, as an object, to the success handler. 如果指定了json,则在将响应作为对象传递给成功处理程序之前,将使用jQuery.parseJSON解析响应。 The parsed JSON object is made available through the responseJSON property of the jqXHR object. 可通过jqXHR对象的responseJSON属性使已解析的JSON对象可用。

So you should be using this: 所以你应该使用这个:

$("#docDepartment").val(data.responseJSON.department)

But to make your code cleaner, It might be better to use the following form: 但是为了使代码更整洁,使用以下形式可能会更好:

getDepartmentByEmp : function (){
    var empName = $.trim($(".temp-test").html());
    console.log(empName);
    var request = $.ajax({
        contentType : "application/json",
        dataType : 'json',
        type : "GET",
        url : "<c:url value='/app/general/add/getDepartment/'/>" + empName,
    })
    request.done(function (data) {
      $("#docDepartment").val(data.department);
      $("#docDepartment").prev().html(data);
      console.log(data);
      console.log(typeof data);
    })
    request.fail(function () {
       ...
    })
},

The main difference is that the done callback should be called with the final data. 主要区别在于done回调应与最终数据一起调用。 While the complete one is called with a jqXHR object. complete对象则用jqXHR对象调用。 It will get called only on success while complete is always called even on errors. 仅在成功时调用它,而在错误时也总是调用complete

If I understand your question correctly, you need to parse the JSON object. 如果我正确理解您的问题,则需要解析JSON对象。 I believe jQuery makes your response JSON automagically. 我相信jQuery会自动使您的响应JSON。 So, the following should work for you. 因此,以下应为您工作。

 $("#docDepartment").val(data.responseText.department);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM