jquery ajax data shows [object Object]

前端 未结 3 514
旧时难觅i
旧时难觅i 2020-12-15 04:28

I have a very basic ajax call to alert the data that was reported from the server

$.ajax({
       type: \"POST\",
       url: \"/someform/act\", //edit utl t         


        
相关标签:
3条回答
  • 2020-12-15 04:52

    You need to use JSON.stringify(data) in the alert to get anything readable.

    Also, $data is a completely different variable name than data.

    0 讨论(0)
  • 2020-12-15 04:55

    If you server send a JSON, you need to put dataType: 'json' to your ajax call. Be aware there's some mistake in your ajax call.

            $.ajax({
                   type: "POST",
                   url: "/someform/act", // NOT 'UTL',
                   data: {
                      key: value,
                      key2: value2
                   },
                   // or data: plaindata, // If 'plaindata' is an object.
                   dataType: 'json',
                   success: function(data) {
                      console.log(data); // As moonwave99 said
                   },
                   error: function() {
                      //error condition code
                   }
            });
    

    EDIT

    When sending data, you should send an object. jQuery will handle the array to sned it to the server. So if plain data is an object, it should be like this

                   data: plainData,
    
    0 讨论(0)
  • 2020-12-15 05:01

    alert() prints the string representation of the arguments - hence if you pass an object, you'll get [object Object].

    To inspect data, use console.log(data) better.

    0 讨论(0)
提交回复
热议问题