2

I have a very basic question (I'm sure) - I have an Zoho application and I'm using their REST API to recover a single result from a table.

I want to use that result in a javascript variable - the form request is here:

<form id="Latest" method="POST" action="https://creator.zoho.com/api/xml/my-company-culture/view/PageFeed_Report">
<input type="hidden" name ="authtoken" value="**********************">
<input type="hidden" name ="scope" id="scope" value="creatorapi">
<input type="submit" value="View Records">
</form> 

I can auto submit the form using this

<script type="text/javascript">
document.getElementById("Latest").submit();
</script>

Which recovers a the result - but I want to assign this result to a javascript variable and use it in a following piece of code (within the same frame).

I am new to this, so please be gentle! Any help appreciated.

5
  • Sounds like Ajax would be your friend. Commented Nov 25, 2013 at 15:15
  • I am so much the beginner - I'll try and get @Ashwin-Balamohan 's jQuery code to work for me first.... :-) Commented Nov 25, 2013 at 17:27
  • I cannot use Ajax (I believe) - as the server being queried is not in the same domain.... Commented Nov 25, 2013 at 18:01
  • Well you can not read the result page because of the other domain, but modern day browsers support CORs. Commented Nov 25, 2013 at 18:06
  • Sadly Zoho does not support CORs....definately a problem they need to address. Commented Nov 26, 2013 at 13:26

1 Answer 1

1

This is easily done with jQuery:

<form id="Latest"> 
    <input type="hidden" name ="authtoken" value="**********************">
    <input type="hidden" name ="scope" id="scope" value="creatorapi">
    <input type="submit" value="View Records">
</form> 

<div id="result"></div> 

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>

    $('#Latest').submit(function(event) {

        // Stop form from submitting normally
        event.preventDefault();

        var url = "https://creator.zoho.com/api/xml/my-company-culture/view/PageFeed_Report";

        // Get some values from elements on the page:
        var $form = $( this );

        var authtokenData = $('#authtoken').attr('value');
        var scopeData = $('#scope').attr('value');

        // Send the data using post
        var posting = $.post( url, 
            { 
                authtoken: authtokenData,
                scope: scopeData
            } 
        );

        // Put the results in a div
        posting.done(function( data ) {

            // empty results div
            $("#result").empty()

            // write POST result to results div
            $("#result").append("<p>" + data + "</p>);

        });             

    });
</script>   
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you SO much for your help. However - I cannot get the 'data' variable to display. I realise that I will have to parse the XML somehow to extract the single value I require, but was hoping to use document.write(data) to display it first. I duplicated your code, substituting the correct authtoken into line 2. I have a ‘view records’ button displayed. (Here is the XML response) <?xml version="1.0" encoding="UTF-8" ?> <response> <records> <record id='1630350000000085025’> <column name="LCI"> <value> <[CDATA[name=85.71&Q1a=1]]> </value> </column> </record> </records> </response>
My pleasure. How do you want the result to look, MarkE?
I have a page which follows a survey the survey takes an entire html string, decodes the URL, and passes it to some google chart calls. The query should return name onwards, which I would like to substitute in, rather than recover the value from the URL. (Hope this makes sense) - here is an example link... culturetransform.com/…
I can generate a new authtoken easily - the current one is d670dc68ac0f6d7ca389e7b206a25045 Again, thank you so much, you are helping more than I could ever expect.
I've created a fiddle - here jsfiddle.net/gz2LG that is returning the following - {"error": "Please use POST request"}, any ideas why this would be? Thank you again for your help....
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.