Adrien Boufflet
Adrien Boufflet

Reputation: 141

jquery ajax data from function

I am trying to retrieve data from a jQuery function.

Here is my function

function getPageHTML() {
    $("input").each(function(){
        $(this).attr("value", $(this).val());
    });
    return "<html>" + $("html").html() + "</html>";
}

above I am getting everything inside my 2 <html> and </html> tags with all values inside my inputs.

function send(){
    $.ajax({
        url:"save-script.php",
        type:'POST',
        data: getPageHTML()
    });
}

Above I am trying to send this data to my save-script.php file. This all works fine but it seems jQuery is interpreting certain character and symbols. The + and the & here.

function getPageHTML() {
    $("input").each(function(){
        $(this).attr("value", $(this).val());
    });
    return "<html>"   $("html").html()   "</html>";
}

my code ends up looking like this after executing the send function. I have tried using dataType: "html" with no success.

also tried data: encodeURIComponent(getPageHTML()) but this ends up removing all my html.

I am trying to keep all these symbols and not have jQuery interpret these symbols.

Upvotes: 1

Views: 76

Answers (4)

Adrien Boufflet
Adrien Boufflet

Reputation: 141

Ok well it seems that I figured out a solution to my problem. I was unable to work my way around with $.ajax and what I was trying to do seemed quiet simple. Send a ajax data: to a .php file. My work around to all this actually made thing a little easier ;)

function send(){
    $.post("save-script.php",
   { getPageHTML }
 );
}

Hope this helps

Upvotes: 0

Cody
Cody

Reputation: 2482

If you just want to turn off encoding, this might be a good option:

processData (default: true)

Type: Boolean

By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

http://api.jquery.com/jquery.ajax/

$.ajax({
    url:"save-script.php",
    type:'POST',
    processData: false,
    data: getPageHTML()
});

Upvotes: 1

Apul Gupta
Apul Gupta

Reputation: 3034

First store return of getPageHTML into a variable & then pass that variable as ajax data.

Your send function should be like this:

function send(){
 var myHTML = getPageHTML();
    $.ajax({
    url:"save-script.php",
    type:'POST',
    data: myHTML
    });
}

Please note that getPageHTML should be defined before send function.

Upvotes: 0

robabby
robabby

Reputation: 2200

You will want to save the output of getPageHtml() to a variable like so:

var html = getPageHtml();

Then pass that new variable as the data of the AJAX call.

var html = getPageHtml();

function send(data){

    $.ajax({
        url:"save-script.php",
        type:'POST',
        data: data
    });
}

send(html);

Try this and let me know if it works.

Upvotes: 1

Related Questions