前提・実現したいこと
現在、Rails5内にWebPackerを利用してjQueryである機能を書いています。
1。ボタンを押すと、アプリ側に位置情報のRequestが飛ぶ
2。アプリ側が受信すると自分の位置情報をRails側にPostする
3。受信した位置情報をRailsのMapに表示する
このようなことを実現したく、個々の機能はだいたい実装できたのですが、非同期処理の部分がうまくいっていません。
$('.user').on('click',function(){ $.when( requestCurrentLocation(clicked_user_id), //ここで位置情報をrequestして、アプリ側からPostしてもらう。 wait(1) // 1秒待ってから成功を返す deferred ).then(function(){ getCurrentLocation(clicked_user_id) //ここで単純に最新の情報を取得する }).done(function(response){ console.dir(response); L.marker([response.last_latitude,response.last_longitude]).addTo(map) .bindPopup(response.display_name +"<br>更新日時:"+ response.current_at) .openPopup(); var latlng = []; latlng.push(response.last_latitude) latlng.push(response.last_longitude) map.panTo(latlng); }).fail(function(xhr, status, error) { console.log("XMLHttpRequest : " + xhr); console.log("XMLHttpRequest : " + status); console.log("errorThrown : " + error); }); });
発生している問題・エラーメッセージ
index.js?aacc:280 XMLHttpRequest : [object Object] index.js?aacc:281 XMLHttpRequest : parsererror index.js?aacc:282 errorThrown : SyntaxError: Unexpected token O in JSON at position 0
となってしまいます。
このように書いたら、当然ですがエラーは出ないのですが、本来の目的である非同期処理がうまくいきません。
requestCurrentLocation(clicked_user_id); $.when(getCurrentLocation(clicked_user_id)).done(function (response){ L.marker([response.last_latitude, response.last_longitude]).addTo(map) .bindPopup(response.display_name +"<br>更新日時:"+ response.current_at) .openPopup(); var latlng = []; latlng.push(response.last_latitude) latlng.push(response.last_longitude) map.panTo(latlng); }).fail(function(xhr, status, error) { console.log("XMLHttpRequest : " + xhr); console.log("XMLHttpRequest : " + status); console.log("errorThrown : " + error); }).always(function(){ });
補足情報(FW/ツールのバージョンなど)
Rails5
jquery
Webpacker
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。