开发者

Parsing secure entries XML file with jquery

Apologies if this is elementary. I'm primarily a front end designer/dev.

I have webform through a form service called wufoo.

Wufoo generates a lovely XML (or json) file that can be grabed and parsed.

I'm trying to grab the entries xml feed that is associated with the form and parse it via jquery to show who has entered.

Im using the following code (which works with a local xml file).

http://bostonwebsitemakeover.com/2/test.html

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script> 
<script> 
$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "people.xml",
        dataType: "xml",
        success: xmlParser
    });
});




function xmlParser(xml) {

    $('#load').fadeOut();

    $(xml).find("Entry").each(function () {

        $(".main").append('<div class="entry">' + $(this).find("Field1").text() + ' ' + $(this).find("Field2").text() + ' http://twitter.com/' + $(this).find("Field17").text() + '</div>');
        $(".entry").fadeIn(1000);

    });



}
</script> 

My XML file contains the following:

<?xml version="1.0"?>
<Entries>
    <Entry>
        <EntryId>1</EntryId>
        <Field1>Meaghan</Field1>
        <Field2>Severson</Field2>
        <Field17/>
    </Entry>
    <Entry>
        <EntryId>2</EntryId>
        <Field1>Michael</Field1>
        <Field2>Flint</Field2>
        <Field17>michaelflint</Field17>
    </Entry>
    <Entry>
        <EntryId>3</EntryId>
        <Field1>Niki</Field1>
        <Field2>Brown</Field2>
        <Field17>nikibrown</Field17>
    </Entry>
    <Entry>
        <EntryId>4</EntryId>
        <Field1>Niki</Field1>
        <Field2>Brown</Field2>
        <Field17>nikibrown</Field17>
    </Entry>
</Entries>

I'm wondering how I would do this with the xml file hosted on the wufoo (which is https)开发者_运维知识库

So I guess Im asking how do I authenticate the feed via jquery? Or do i need to do this via json? Could someone explain how?


The problem is the same-origin policy. This is a rule, enforced by all browsers, that you cannot use XMLHTTPRequest (the basis for AJAX) cross-domain. You may not make requests to another server, or to the same server if it uses a different port or protocol (http/https, for example).

The most plausible solution is to set up a script on your web server that proxies the XML file for your application. In PHP, for instance, it could be as simple as:

<?php
header('Content-Type: application/xml');
echo file_get_contents('the wufoo url');
?>

You could then call the file with AJAX and receive the contents of the remote file.


jQuery allows for a username and password to be passed into the ajax call:

$(document).ready(function () {
$.ajax({
    type: "GET",
    url: "people.xml",
    dataType: "xml",
    success: xmlParser,
    username: "myUsername"
    password: "myPassword"
});
});

However this puts you username and password in plain text in your js. You may want to think about setting up a little php proxy to make the authenticated call so that all your credentials are only on the server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜