2016-04-16 11 views
2

WP-REST API v2とAngularJSのhttpでWordPressブログにコメントを追加しようとしています。 GETリクエストでは、すべて正常に動作します。

このように(URLにパラメータを追加する)POSTリクエストを使用すると、すべて正常に動作し、コメントがデータとともに追加されます。

$http({ 
    method: 'POST', 
    url: 'http://myblog.com/json/wp-json/wp/v2/comments?author_name=Myself&content=Hello guys', 
    headers: { 
    'Content-Type': undefined 
    } 
}).then(function (res) { 
    console.info('[REST] POST request sent to "' + route + '"'); 
    d.resolve(res); 
}, function (err) { 
    console.error('[REST] POST request failed. Error message: ', err); 
    d.reject(err); 
}); 

しかし、私は(マニュアルに従って)$ http.getのために、「データ」のパラメータで、このようにそれを使用する場合は、コメントがワードプレスに追加しますが、それは空のさ。内容や名前なし。

$http({ 
    method: 'POST', 
    url: 'http://myblog.com/json/wp-json/wp/v2/comments', 
    headers: { 
    'Content-Type': undefined 
    }, 
    data: { 
    author_name: 'Myself', 
    content: 'Hello guys' 
    } 
}).then(function (res) { 
    console.info('[REST] POST request sent to "' + route + '"'); 
    d.resolve(res); 
}, function (err) { 
    console.error('[REST] POST request failed. Error message: ', err); 
    d.reject(err); 
}); 

なぜそれは第二の方法を動作しませんか?または、URLに問い合わせるパラメータでそれを行う必要がありますか?

よろしくお願いいたします。

答えて

3

私はそれを解決しました。 application/x-www-form-urlencoded Content-Typeを使用し、データにAngularの$httpParamSerializerJQLikeを使用する必要がありました。

$http({ 
    method: 'POST', 
    url: self.address + route, 
    headers: { 
    'Content-Type': 'application/x-www-form-urlencoded' 
    }, 
    data: $httpParamSerializerJQLike(params) 
}).then(function (res) { 
    console.info('[REST] POST request sent to "' + route + '"'); 
    d.resolve(res); 
}, function (err) { 
    console.error('[REST] POST request failed. Error message: ', err); 
    d.reject(err); 
}); 
関連する問題