[Bug Report] dependent() when called with a object in fetch parameter, casts to string

[Bug Report] dependent() when called with a object in fetch parameter, casts to string

pcjacksonpcjackson Posts: 18Questions: 6Answers: 0
edited November 2019 in Free community support

Tested in Editor 1.9.2

Per the dependent() documentation, if you have an object in the fetch field:

As an object, this value is used as the options to make an Ajax request using jQuery's $.ajax() method. Please note that it is important you specify a url option otherwise the Ajax request will have nowhere to go! The format of the data submitted and the JSON data returned is discussed above.

However, the actual behavior experienced when passing the following object to dependent() is a request to "[object Object]".

{
  "url": "/Ajax/ClassOptions",
  "headers": {
    "__RequestVerificationToken": "..."
  },
  "data": {
    "__RequestVerificationToken": "..."
  }
}

Replies

  • pcjacksonpcjackson Posts: 18Questions: 6Answers: 0
    edited November 2019

    The cause of the bug is this section of javascript:

    if ( $.isPlainObject( url ) ) {
        $.extend( ajaxOpts, url );
    }
    else {
        ajaxOpts.url = url;
    }
    
    $.ajax( $.extend( ajaxOpts, {
        url: url,
        data: data,
        success: update
    } ) );
    

    As you can see, the ajaxOpts object is properly updated if the url is a plainObject. But when the call is actually made, the url is overwritten. Probably the easiest way to fix this would be commenting out the url: url, line. And that's what I've done temporarily. And for my use case it was also necessary to extend the data field.

    if ( $.isPlainObject( url ) ) {
        $.extend( ajaxOpts, url );
    }
    else {
        ajaxOpts.url = url;
    }
    
    $.ajax( $.extend( ajaxOpts, {
        // url: url,
        data: $.extend(ajaxOpts.data, data),
        success: update
    } ) );
    
  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    Thank you for the analysis! This will be fixed in 1.9.3.

    Allan

  • loukinglouking Posts: 259Questions: 52Answers: 0

    I believe I am still seeing this in 1.9.6. Hmm it's kind of blocking me. Should I just use the fix suggested by @pcjackson ?

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    Apologies - it looks like I missed this one.

    Yes, I concur that @pcjackson's fix is the correct one here. It can be applied locally until we deploy the fix.

    Allan

This discussion has been closed.