已成功添加“获取记录”。[object Object]而不是实际值

时间:2014-12-16 21:57:02

标签: javascript jquery ajax json

下面的脚本通常会将格式化为JSON对象的值返回,如下所示:

{"Value":null,"Status":2,"Message":"Greetings."}

过去几天,我们一直在:

Record has been added successfully.**[obect Object]**

任何可能出错的想法?​​

这是下面的代码。

 <script type="text/javascript">

    function CallService() {

     //Creating an object to hold token form field
     myToken = new Object();
     myToken.Token =  $("#token").val();

     //Creating an object to hold form fields
     myData = new Object();

      // Creating variables to hold data from textboxes. First building associated details
      myData.Address = $("#Address").val();
      myData.CallerAcctNum = $("#CallerAcctNum").val();
      myData.CallerAddress = $("#CallerAddress").val();
      myData.CallerCellPhone = $("#CallerCellPhone").val();
      myData.CallerCity = $("#CallerCity").val();
      myData.CallerComments = $("#secondarysitecontact").val();
      myData.CallerDistrict = $("#CallerDistrict").val();
      myData.CallerEmail = $("#CallerEmail").val();
      myData.CallerFax = $("#CallerFax").val();
      myData.CallerFirstName = $("#CallerFirstName").val();
      myData.CallerHomePhone = $("#CallerHomePhone").val();
      myData.CallerLastName = $("#CallerLastName").val();
      myData.CallerMiddleInitial = $("#CallerMiddleInitial").val();
      myData.CallerState = $("#CallerState").val();
      myData.CallerWorkPhone = $("#CallerWorkPhone").val();
      myData.CallerZip = $("#CallerZip").val();
      myData.City = $("#City").val();
      myData.Details = $("#comments").val();
      myData.District = $("#District").val();
      myData.Location = $("#Location").val();
      myData.ProblemSid = $("#RequestID").val();
      myData.State = $("#State").val();
      myData.StreetName = $("#StreetName").val();
      myData.X = $("#X").val();
      myData.Y = $("#Y").val();
      myData.SiteContactDisplay = $("#sitecontact").val();
      myData.Comments = $("#comments").val();
      myData.Text1 = $("#deptId").val();
    $.ajax({
        type: "POST",
        url: "proxyCreate.php",
        data: {
            data: JSON.stringify(myData),
           token: myToken.Token
        },
        dataType: "json",
        async: false,
        success: function (response) {
          alert("Record has been added successfully." +  response  );
            window.location.reload();
        }
    });
      return false;
     }
    </script>

2 个答案:

答案 0 :(得分:2)

除非 JSON.stringify ,否则

alert() 无法打印 {}对象[object Object]
如果您通常从此成功返回此对象:

{"Value":null,"Status":2,"Message":"Greetings."}
来自response参数的

,而不是您需要访问所需的Object属性,如:

alert( response.Message );

否则,如果你想完全阅读该对象,请执行以下操作:

alert( JSON.stringify( response, null, "\t") );

此外,为了简化您的代码,我做了一些更改:

function v(id){ return $("#"+id).val(); } // Get value
function CallService() {

  var myToken = v("token");
  var myData  = {
    Token               : v("token"),
    Address             : v("Address"),       
    CallerAcctNum       : v("CallerAcctNum"),
    CallerAddress       : v("CallerAddress"),
    CallerCellPhone     : v("CallerCellPhone"),
    CallerCity          : v("CallerCity"),
    CallerComments      : v("secondarysitecontact"),
    CallerDistrict      : v("CallerDistrict"),
    CallerEmail         : v("CallerEmail"),
    CallerFax           : v("CallerFax"),
    CallerFirstName     : v("CallerFirstName"),
    CallerHomePhone     : v("CallerHomePhone"),
    CallerLastName      : v("CallerLastName"),
    CallerMiddleInitial : v("CallerMiddleInitial"),
    CallerState         : v("CallerState"),
    CallerWorkPhone     : v("CallerWorkPhone"),
    CallerZip           : v("CallerZip"),
    City                : v("City"),
    Details             : v("comments"),
    District            : v("District"),
    Location            : v("Location"),
    ProblemSid          : v("RequestID"),
    State               : v("State"),
    StreetName          : v("StreetName"),
    X                   : v("X"),
    Y                   : v("Y"),
    SiteContactDisplay  : v("sitecontact"),
    Comments            : v("comments"),
    Text1               : v("deptId")
  };

  $.ajax({
    type: "POST",
    url: "proxyCreate.php",
    data: {
      data: JSON.stringify(myData),
      token: myToken
    },
    dataType: "json",
    async: false,
    success: function (response) {
      console.log( response ); // open console and take a look.
      alert("Record has been added successfully." +  response  ); // Nest with "." like response.something to print property you need
      // window.location.reload(); // Do this later after you fix the bug
    }
  });
  return false;

}

答案 1 :(得分:0)

您必须指定要打印的对象的哪个部分,例如,如果您想要消息:

 $.ajax({
        type: "POST",
        url: "proxyCreate.php",
        data: {
            data: JSON.stringify(myData),
           token: myToken.Token
        },
        dataType: "json",
        async: false,
        success: function (response) {
          alert("Record has been added successfully." +  response.Message  );
            window.location.reload();
        }
    });

你正在尝试使用alert,它接受一个字符串并将其打印在一个对话框上,然后给它一个对象,它......它不是一个字符串。你的对象包含字符串,所以你可以访问它们并打印它们,而不是试图打印整个对象,比如上面代码中的show,我转到响应对象的message属性。

更简单的例子:http://jsfiddle.net/9c0xLwcq/