-1

私はjQueryの非常に奇妙な振る舞いのように思われることに髪を引っ張っています。

 define ['jquery', 'models/ConfigModel'], ($,  ConfigModel) ->
   class MyController
     initialize: () ->
       confData = $.getJSON('/config.json', (data) -> console.log data)

これは問題なく機能します。config.jsonのコンテンツを返します

   define ['jquery', 'models/ConfigModel'], ($,  ConfigModel) ->
     class MyController
       initialize: () ->
         confData = $.getJSON
           url: '/config.json' 
           success: (data) -> console.log data

これにより、index.htmlのコンテンツが返されます。これは、サーバーの.htaccessファイルが原因である可能性があります。

   RewriteEngine On
   RewriteRule ^(js|css|media|api|pie|templates|config.json) - [L,NC]
   RewriteCond %{REQUEST_FILENAME} !index.html
   RewriteRule .* index.html [QSA,L]

ChromeコンソールでJSを使用してみましたが、同じ結果が得られました。

   //works well
   $.getJSON('/config.json', function(data) {console.log(data)}) 
   //index.html
   $.getJSON({url:'/config.json', success: function(data) {console.log(data)}}) 

私には回避策があるので、このような状況をデバッグする方法に関する他の人の考えよりも、解決策の重要性は低くなります。

編集:

    $.getJSON({url:'/config.json', success: function(data) {console.log(data)}})
    XHR finished loading: "http://site.dev/[object%20Object]]".

     $.getJSON('/config.json', function(data) {console.log(data)})
     XHR finished loading: "http://site.dev/config.json".
4

1 に答える 1

1

これはのフットプリントですgetJSON

jQuery.getJSON( url [, data ] [, success(data, textStatus, jqXHR) ] )

これをドキュメントからコピーしました。この関数を思い通りに使用しないでください。

$.ajax()すべての設定でオブジェクトを渡す場合に使用する必要があります。

于 2013-03-11T23:23:31.160 に答える