2017-08-18 1 views
1

代わりに次のjqueryコードを翻訳して、フェッチAPIを使用しようとしました。私は空の配列を取得するノードを持つ端末にそれをconsole.logとき

fetch('/{{user.username}}', { 
    method: 'PUT', 
    headers: { 
    'Content-Type': 'application.json' 
    }, 
    body: JSON.stringify({ 
    street: document.getElementById("street").value, 
    city: document.getElementById("city").value, 
    state: document.getElementById("state").value, 
    zip: document.getElementById("zip").value 
    }) 
}).then(() => { 
    cancel() 
    location.reload() 
}) 
} 

function save() { 
    $.ajax('/{{user.username}}', { 
    method: 'PUT', 
    data: { 
     street: $('#street').val(), 
     city: $('#city').val(), 
     state: $('#state').val(), 
     zip: $('#zip').val() 
    }, 
    complete: function() { 
     cancel() 
     location.reload() 
    } 
    }) 
} 

これは、APIリクエストをフェッチされています。それはPUT要求を行います。

私は、次のとExpressでそれを処理しようとしています:

app.put('/:username', function (req, res) { 
    console.log(req.body) 
    console.log("hello") 
    var username = req.params.username 
    var user = getUser(username) 
    user.location = req.body 
    saveUser(username, user) 
    res.end() 
}) 

答えて

0

そうでない場合はjQueryのバージョンが動作しないので、私はあなたがExpressでbodyParserを使用していると仮定します。

headers: { 
    'Content-Type': 'application.json' 
}, 

headers: { 
    'Content-Type': 'application/json' 
}, 
する必要があります
関連する問題