1

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.

7
  • First store return of getPageHTML into a variable & then pass that variable as ajax data. Commented Mar 3, 2015 at 18:44
  • Can you tell us what you're trying to accomplish? It's not really clear to me what you're trying to do from your code. Commented Mar 3, 2015 at 18:44
  • yes. every concatenated are missed out. I am just trying to copy my html without any interpretation. I have read about Jquery dataType but this doesn't work out if I use html Commented Mar 3, 2015 at 18:45
  • It's unclear what is the problem. How is the result of getPageHTML() look like and what's wrong with it? Commented Mar 3, 2015 at 18:50
  • getPageHTML() is simply getting everything inside my 2 html tags. when using the ajax function, I am missing out symbols such as the + and the &. all other html seems fine Commented Mar 3, 2015 at 18:52

4 Answers 4

1

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.

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

8 Comments

Yes I had already tried that one out but I get "getPageHtml is not defined".
Same problem. if I put my variable outside my function, it doesn't seem to find it.
@AdrienBoufflet Perhaps post your updated code to a jsbin or something so I can help you debug. What I have posted here should work with no problems
silly me. I am sending a request with ajax to a .php file. this isn't possible with jsffidle if I am right?
It doesn't matter if the URL it posts to actually exists. We can still examine the request in the chrome debugger or something similar. Here: jsfiddle.net/k6zdd3a6 Compare what gets logged in the console with the network request: i.imgur.com/8RI0nKG.png They look the same to me...
|
1

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()
});

4 Comments

thanx for the response but doesn't seem to work. ajax is still interpreting certain symbols and characters. work around was using .replace(new RegExp( "\\+", "g" ),"%2B").replace(new RegExp( "\\&", "g" ),"%26" but this doesn't solve my problem. if I try and copy the copied page, the symbols will have been replaced
I think it's the $.html call that's doing the escaping, not $.ajax
I have tried executing the function with a simple onclick="console.log(getPageHTML()); and all is fine. I get everything inside my 2 html tags without any encoding
it is the $.ajax that is encoding certain characters or more like not interpreting these characters. If I execute the PageHTML() function, all characters and symbols are shows in my console log since I am using $("html").html(). the encoding happens when $.ajax is sent.
0

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.

Comments

0

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

Comments

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.