PolymerElements / iron-ajax

Easily make ajax requests
https://www.webcomponents.org/element/PolymerElements/iron-ajax
127 stars 113 forks source link

Different relative URLs depending on data binding #259

Open SPessall opened 7 years ago

SPessall commented 7 years ago

Description

When using relative paths the absolute URL of a generated request seems to be different depending on whether data binding is used.

When using data binding the absolute URL will be relative to the current window location:

    <iron-ajax url="../[[service]]" last-response="{{gateList}}" auto></iron-ajax>

In my case this component generates a request to http://localhost:8080/gate-list if the 'service' property has the value 'gate-list'.

When using a fixed relative URL the resulting absolute URL seems to depend on the source file location.

    <iron-ajax url="../gate-list" last-response="{{gateList}}" auto></iron-ajax>

This component generates a request to http://localhost:8080/src/gate-list, containing an additional level "src/".

Expected outcome

Both variants should generate requests to the same absolute URL.

Actual outcome

The absolute URLs of the generated requests are different, as noted above.

Browsers Affected

azariah001 commented 7 years ago

Hello

This is specifically an eccentricity of the Polymer binding system and not an error on the part of this component. I've actually encountered this issue before across multiple components, was something of a learning curve, in the beginning. A solution is to bind complete data strings which is good for code specificity and allows for easier debugging if you ever change site structure. The reason for this behavior is that when you enter a binding in front of a property the operation doesn't take into account any other characters inside of the binding string it literally just binds in a property = binding way, not in the expected format string way property = `../${binding}`.

On a related topic I'm somewhat surprised that you're able to use relative links at all as it was my understanding that xhr only supported absolute values that included the domain and protocol, and in fact I just wrote a monolithic _urlChanged Observer for iron-ajax that I'm using in my project to handle all of the possible URL formats that can be used.

EDIT: Excuse me while I go eat my hat... 😨 About the only potentially useful thing I wrote in my observer might be the automatic upgrading of HTTP URL's when attempted to be used in a HTTPS page. But other than that.... dumb dumb here wasted 2 hours.... I swear the way I'm now doing my links wasn't working last I checked... url="/query". Well, learning things every day.

stramel commented 6 years ago

I'm not able to reproduce, can one of you provide a jsfiddle or plunker that can reproduce this issue?