2017-04-16 9 views
0

オートコンプリート機能のJSONをリクエストしています。残念ながら、JSONはhtmlスパンタグでラップされています。これは、VBで記述された貧弱なバックエンドライブラリのために発生します。これは、数ヶ月以内にオペレータによって変更されません。だから、別の方法でhtmlスパンタグを取り除かなければならない。私のJSコールとレスポンスは以下の通りです:jQuery JSONレスポンスがHTMLでラップされています

module.props.functions.ajaxSearchon = function (currVal) { 
    // Search on Type 
    $.ajax({ 
     url: module.props.apiURLs.searchOnType + '?query=' + currVal + '&lang=' + module.props.lang, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (result) { 
     module.state = { 
      searchResult: result, 
      searchQuery: currVal 
     }; 
     }, 

     error: function (xhr, status, error) { 
     module.state = $.extend(module.state, { 
      searchResults: 'no results found' 
     }); 
     console.log("error", module.state); 
     }, 

     complete: function() { 
     module.props.functions.refreshSearchResults(); 
     } 
    }) 
    }; 

これは応答として整形式JSONで優れています。しかし、私の応答は次のようになります。だから、

<span id="fromContext"> 
{ 
    "results": [ 
    { 
    "headline": "headline text", 
    "summary": " <em>summary</em> result text", 
    "url": "/url/to/page", 
    "image": { 
     "url": "/media.jpg", 
     "alt": "media alt text" 
    }, 
    "count": "" 
    }, 
    { 
    "headline": "empty object" 
    } 
    ], 
"hasmore": true 
} 
</span> 

(それはIDを持っているので)、私はスパンを検出することができるだろうが、私は、Ajaxリクエストに応答全体のJSONの一部にアクセスする方法を知りません。私は、この主な問題は、応答がJSONではないため、エラーでajax呼び出しが発生すると考えています... どのようにこの問題を解決するためのアイデア?

答えて

0

あなたはajax.dataType属性を変えることができる "テキスト" とcontentTypeには "text/plainの"

contentType: "text/plain; charset=utf-8", 
    dataType: "text", 

とjQueryオブジェクトへの応答文字列を変換

const $responseObject = $(response); 

、その後、有効なjsonに

let text = $responseObject.text(); 

でアクセスしてさらに処理することができます。

関連する問題