0

現在、2 つの Web サービスがあります。1 つは私のメイン アプリで、もう 1 つは JSON データを返します。投稿への応答として取得した JSON データの値に基づいて div を表示/非表示にしようとしています。このロジックを追加する最良の方法/場所はどこですか? ここに私がこれまでに持っているもののいくつかのコードがあります。ajax 呼び出しは正常に機能しており、応答に基づいてメッセージをレンダリングできますが、ajax 応答に基づいて div を表示/非表示にするロジックについてはわかりません。ありがとう。

コントローラーのアクション:

def checkItemProperty() {
  def service = new MyService()
  def itemInstance = new Item(params)
  String itemProperty = itemInstance.itemProperty

  if (service.checkItemCondition(itemProperty)) {
    render "Property is true"
  }
  else {
    render "Property is false"
  }
}

gsp ヘッド スニペット:

<g:javascript library="jquery" />
<g:javascript>
  $(document).ready(function() {
    $("#showHideDiv").hide();
    $("#someClickableLink").click(function(){ $("#showHideDiv").show(); });
  });
</g:javascript>

gsp 本文スニペット:

<g:formRemote name="testForm" url="[action:'checkItemProperty']" update="[success: 'message', failure: 'error']">
  <g:textField name="itemProperty" value="${itemInstance?.itemProperty}" />
  <g:actionSubmit type="submit" name="add" value="Check" />
  <span id="message"></span>
  <span id="error"></span>
</g:formRemote>

<div id="showHideDiv">...</div>

編集: サービス方法:

def checkItemCondition(String itemProperty) {
  def test = new RESTClient('http://localhost:8081/test/')
  def testResponse = test.post(path : 'test.json', body : [status:itemProperty, source:'httpbuilder'], requestContentType : URLENC)
  def jsonObject = testResponse.getData()
  return jsonObject['itemResponse']
}

編集: 上記のコントローラーアクションのJSONデータをどのように構築したか:

def test = [:]
test.value = true
test.text = 'Property is true'
render test as JSON
4

2 に答える 2

5

コントローラーを変更する必要があります。

コントローラ:

render [value: true, text: "Property is true"] as JSON

また

render [value: false, text: "Property is false"] as JSON

GSP (update属性を削除):

<g:formRemote ... onSuccess="handleResponse(data)">

JavaScript:

function handleResponse(data) {
$("#message").html(data.text);
if (data.value) {
 $("#showHideDiv").show();
}
else {
 $("#showHideDiv").hide();
}
}
于 2013-05-22T18:35:52.900 に答える