Forum Discussion

jonathancrow's avatar
13 years ago

How to redirect the response from a REST API Call

So close...

 

Ok I have a form that posts the content of a textarea to the body of a specific article. The problem is that after submitting the form, the browser displays the XML response from the REST API call.

 

The actual REST call works. I just need to take the response and either not display it or do something like redirect to the article page.

 

I tried appending: "&redirect_uri=${my_url}" to the API call but it didn't work.

 

Any suggestions?

 

More detailed code below...

 

thanks,

Jonathan

 

<form action="${my_community}/restapi/vc/messages/id/${my_topic_id}/reply?message.subject=${topic} method="POST" >
<p>
<textarea name="message.body" cols="80" rows="5"></textarea>
</p>
<p>
<label>
<input type="submit" value="Post Reply">
</label>
</p>
</form>

  • xorrkaz's avatar
    xorrkaz
    13 years ago

    Try this:

     

    <@liaAddScript>
    function handleSubmit(form) {
      ;(function($) {
        $.ajax({
          type: "POST",
          url: "${my_community}/restapi/vc/messages/id/${my_topic_id}/reply",
          data ({
            "message.body":form['message.body'].value,
            "message.subject":"${the_topic}"
          })
        });
      })(LITHIUM.jQuery);
    
      // note: my_url is a Javascript variable
      // use ${my_url} if your variable is in Freemarker
      window.location.href = my_url;
    }
    </@liaAddScript>

     

  • We do a lot with embedded forms and REST, but we use AJAX to do the work.  By doing this, we can control the behavior of the page on the client side.  Instead of your submit, what about an onSubmit handler?  This need not be asynchronous, but it can be.  Here's an example:

     

    <form onSubmit='handleSubmit(this); return false'>
    ...
    </form>
    <@liaAddScript>
    function handleSubmit(form) {
        ;(function($) {
            $.ajax({
                type: "POST",
                url: "${my_community}/restapi/vc/messages/id/${my_topic_id}/reply?message.subject=${topic}",
                data ({
                    "message.body":form['message.body'].value
                })
            });
        })(LITHIUM.jQuery);
    }
    </@liaAddScript>

     

    • jonathancrow's avatar
      jonathancrow
      Advisor

      xorrkaz,

       

      Thanks, I was looking at ajax, but i am not skilled enough in coding to figure it out. I have been trying to tweak code from various sources but just can't get it to work.

       

      I tried using the code you provided and had two issues - getting the info to post, and getting the redirect.

       

      1. As for the post, I hit the button and the page goes away and comes back. The form data has dissappeared. Leading me to hope that the data was posted. But going to the article I don't find a reply. The code I used is posted below.

       

      2. Once the data is posted how do I redirect? Unless I am totally hopeless I didn't see anything for a redirect. Were you assuming i was smart enough to figure that out on my own (in which case I am just lost;) or is there something in the code I don't understand?

       

      Thanks again for the help.

       

      Cheers,

      Jonathan

       

       

       

      Here is the full source I used:

      <@liaAddScript>
      function handleSubmit(form) {
      ;(function($) {
      $.ajax({
      type: "POST",
      url: "${my_community}/restapi/vc/messages/id/${my_topic_id}/reply?message.subject=${the_topic}",
      data ({
      "message.body":form['message.body'].value
      })
      });
      })(LITHIUM.jQuery);
      }
      </@liaAddScript>
      <!-- Create a form for replies and comments -->

      <form name="reply_form" onSubmit='handleSubmit(this);'>
      <p>
      <textarea name="message.body" cols="80" rows="5"></textarea>
      </p>
      <p>
      <label>
      <input type="submit" value="Post Reply" />
      </label>
      </p>
      </form>


      • xorrkaz's avatar
        xorrkaz
        Genius

        I think maybe I didn't understand what you meant by redirect.  I assumed you wanted to submit the form without changing the current page.  In that case, you can acomplish what with:

         

        <form name="reply_form" onSubmit='handleSubmit(this); return false;'>

         

        You'll also need a few changes based on the API:

         

        <@liaAddScript>
        function handleSubmit(form) {
          ;(function($) {
            $.ajax({
              type: "POST",
              url: "${my_community}/restapi/vc/messages/id/${my_topic_id}/reply",
              data ({
                "message.body":form['message.body'].value,
                "message.subject":"${the_topic}"
              })
            });
          })(LITHIUM.jQuery);
        }
        </@liaAddScript>

         

        As for syntax checking, if you're using Firefox, check Tools > Web Developer > Error Console after submitting to see if there are any Javascript errors.