ExpressJS で ajax 呼び出しを他の呼び出しと区別するのに苦労しています。
私が理解している限りrequest.accepts('json')
、jsonリクエストを識別するために使用できますか?
問題は、どうやら、すべての呼び出しがすべてを受け入れることです!
app.get( '*', function(request, response, next ) {
console.log('request accepts:')
if( request.accepts( 'json' ) ){
console.log( '--> accepts json' )
}
if( request.accepts( 'html' ) ){
console.log( '--> accepts html' )
}
if( request.accepts( 'blah' ) ){
console.log( '--> accepts blah' ) // this does not show up
}
if( request.accepts( 'application/json' ) ){
console.log( '--> accepts json2' )
}
next()
} )
ページにアクセスしただけでは、json と html が受け入れられます。
を使用しようとする$.getJSON( ... url ... )
と、json と html も受け入れます。
Headers:
Browser: "Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
Ajax: "Accept application/json, text/javascript, */*; q=0.01"
私は受け入れヘッダーの専門家ではありませんが、その*/*
部分が問題になる可能性があるようです。
ExpressJSで正しい(またはおそらく最初の)受け入れタイプを決定するにはどうすればよいですか? あるいは、JSON リクエストと通常のページ訪問を区別するにはどうすればよいですか?