最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - window.URLSearchParams broken on Edge - Stack Overflow

matteradmin4PV0评论

I'm trying something like the following;

let formUrl = new window.URLSearchParams(
        new window.FormData(this.searchForm)
      ).toString()

But I'm getting '0: Invalid argument'

This works on every browser apart from edge.

Has anyone run into this issue before

I'm trying something like the following;

let formUrl = new window.URLSearchParams(
        new window.FormData(this.searchForm)
      ).toString()

But I'm getting '0: Invalid argument'

This works on every browser apart from edge.

Has anyone run into this issue before

Share Improve this question asked Sep 26, 2019 at 12:45 Paul MackeyPaul Mackey 959 bronze badges 1
  • Which Edge version are you trying? Did you try to print each of the parts of your statement to see if something is undefined in Edge? – callback Commented Sep 26, 2019 at 12:54
Add a ment  | 

2 Answers 2

Reset to default 6

The problem in Edge is that they do not support destructuring of FormData object. (for ... of in linked table).

Indeed, you are relying on this behavior from other browsers in your code, because URLSearchParams( init ) constructor accepts a record as init parameter.

The specs read

  1. Otherwise, if init is a record, then for each namevalue in init, append a new name-value pair whose name is name and value is value, to query’s list.

So this means that your code is actually doing

const formData = new FormData( searchForm );

const urlParams = new URLSearchParams();
for ( let [ name, value ] of formData ) {
  urlParams.append( name, value );
}
let formUrl = urlParams.toString();

console.log( formUrl );
<form id="searchForm">
  <input name="foo" value="bar">
  <input name="baz" value="bla">
</form>

But since, once again, Edge doesn't support destructuring of FormData, your code breaks.

However, according to MDN, they do support for ... of FormData.entries(), so maybe this would work (no Edge to test though so let me know).

let formUrl = new window.URLSearchParams(
  new window.FormData(this.searchForm)
    .entries() // explicitely call .entries() to get the iterator
).toString()

console.log( formUrl );
<form id="searchForm">
  <input name="foo" value="bar">
  <input name="baz" value="bla">
</form>

A nice answer to another question that gives you the right code is here: https://stackoverflow./a/44033425/1568714

Basically, do this

new URLSearchParams(Array.from(new FormData(formElement))).toString()

Kaiido's answer explains the problem well, but using that approach you will get issues in iOS safari versions < 10.3 and chrome versions < 53, where .entries is not supported. (https://caniuse./#feat=object-entries)

Post a comment

comment list (0)

  1. No comments so far