LoginSignup
0
2

More than 5 years have passed since last update.

JQuery ajaxでデータをJerseyに送信する時に間違いやすい内容に出会った件

Posted at

依存の追加


        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
        </dependency>

この依存が必要です。

FormDataParam != FormParam

@FormParamapplication/x-www-form-urlencodedのパラメータに対応していて、FormDataParammultipart/form-dataに対応しています。間違えたら、500番のエラーで返してきます。

ContentType

ajax内のcontentTypeを'multipart/form-data'に設定してはいけません。falseに設定すべきです。
その違いはChromeのデベロッパーツールで調べれば一目瞭然です。


Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:ja,en-US;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:130686
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryiErKBUuHJyGS9P3k
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/?
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36
X-Requested-With:XMLHttpRequest

boundaryが勝手につけられました。こいつが必須です。入っていないリクエストをサーバー側では受け付けません。

サンプル

Sending multipart/formdata with jQuery.ajax

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2