1

Hi there (& Happy New Year!)

Are there some examples on how I can use JQUERY to get XML from a remote REST API and just display the XML? I just need a little assistance to get things going.

Request Details:

https://{username}:{password}@api.opsourcecloud.net/oec/0.9/myaccount 

Response Details:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<ns3:Account xmlns:ns2="http://oec.api.opsource.net/schemas/organization" .. >     
<ns3:userName>rdyer</ns3:userName> 
    <ns3:fullName>Joe Public</ns3:fullName> 
    <ns3:firstName>Joe</ns3:firstName> 
    <ns3:lastName>Public</ns3:lastName> 
    <ns3:emailAddress>[email protected]</ns3:emailAddress> 
    <ns3:orgId>1831c1a9-9c03-44df-a5a4-f2a4662d6bde</ns3:orgId> 
    <ns3:roles> 
        <ns3:role> 
            <ns3:name>primary administrator</ns3:name> 
        </ns3:role> 
    </ns3:roles> 
</ns3:Account> 

2 Answers 2

7

Use the jQuery.get method.

For example:

$.get(
    'https://{username}:{password}@api.opsourcecloud.net/oec/0.9/myaccount',
    function(data) { alert(data); }
);

EDIT: For security reasons, you cannot use AJAX to get data from a different domain. Therefore, you'll need to write a server-side script to get the data from the other domain, then call that using $.get.

Sign up to request clarification or add additional context in comments.

2 Comments

For some reason, it's not displaying the data. I tried it with a different external URL, and it didn't work either. $.get('ajax.googleapis.com/ajax/services/search/… { alert("DATA LOADED: " + data); }); Any ideas why?
For me, it did not display the data due to timeout. I havn't figured out how to fix this issue yet.
0

If you just want to display the results of the REST service and you don't care about format or anything, this is what you can do:

        <script>
        ....

        $.ajax('<your_rest_service_url>', {
            dataType:'xml',
            data:{},
            type:'GET',
            success:function(data, status, response) {
                var tmp=response.responseText; // THIS IS THE TRICK
                $('#result').text(tmp);

         ....
         </script>

        <span id="result"></span>

The trick is NOT use the "data" parameter (like you're suppose to.... and what everyone else on the internet is telling you to do). Just remember, this is quick and dirty.

1 Comment

The data property is for sending data to the server not for processing the response per JQuery API: data Type: PlainObject or String Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting

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.