admin管理员组

文章数量:1313121

I am struggling with the post() method.

I've been reading several posts on here and the jquery forums, and just trying to get a simple piece of code working is proving difficult. My ultimate goal is to pass a div #exportData and all it's children to test.php.

I started with:

$.post("ajax/test.php", $("body").html());

To my mind this should return the entire contents of the current page to test.php (unless test.php requires a holding div or element to receive the content). Currently it only returns the blank page.

I then tried looking at the parameters for post() if I need to manipulate these:

$.ajax({
        type: 'POST',
        url: ajax/test.php,
        data: data,
        success: success,
        dataType: dataType
    });

Also declared a variable:

var data = { 
        html: #exportData
    };

That bit failed of course. I don't know how to define the data in the parameters, or if this is the right place to do it.

Although I would have thought if:

 $.post("ajax/test.php", $("body").html());

would have worked then presumably I can substitute "body" for any class, id or selector I like.

Also does the submit button need certain parameters to tie it to the post function. At the moment it is purely:

<input type="submit" id="submit" value="send" name="submit">

Sorry this is such a basic question.

I am struggling with the post() method.

I've been reading several posts on here and the jquery forums, and just trying to get a simple piece of code working is proving difficult. My ultimate goal is to pass a div #exportData and all it's children to test.php.

I started with:

$.post("ajax/test.php", $("body").html());

To my mind this should return the entire contents of the current page to test.php (unless test.php requires a holding div or element to receive the content). Currently it only returns the blank page.

I then tried looking at the parameters for post() if I need to manipulate these:

$.ajax({
        type: 'POST',
        url: ajax/test.php,
        data: data,
        success: success,
        dataType: dataType
    });

Also declared a variable:

var data = { 
        html: #exportData
    };

That bit failed of course. I don't know how to define the data in the parameters, or if this is the right place to do it.

Although I would have thought if:

 $.post("ajax/test.php", $("body").html());

would have worked then presumably I can substitute "body" for any class, id or selector I like.

Also does the submit button need certain parameters to tie it to the post function. At the moment it is purely:

<input type="submit" id="submit" value="send" name="submit">

Sorry this is such a basic question.

Share Improve this question edited Dec 22, 2015 at 20:24 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 29, 2012 at 11:03 lharbylharby 3,2656 gold badges26 silver badges65 bronze badges 1
  • You'll need to show the code for the page ajax/test.php in order for us to help. I think you have some further exploring to do around web forms in the first place, this will help you understand how the submit button works and how it's all linked together. – Lazarus Commented Feb 29, 2012 at 11:07
Add a ment  | 

3 Answers 3

Reset to default 3

You could do

var html = $("body").html();
var data = { 
        html: html
    };
$.post("ajax/test.php", data);

as the second parameter of $.post() is an object wich contains the data you want to send to the server.

To send the data you could do:

<input type="submit" id="submit" value="send" name="submit">

js

$('input#submit').click(function(e){
  //prevent submitting the form (if there is a form)
  e.preventDefault();
    var html = $("body").html();
    var data = { 
            html: html
        };
    $.post("ajax/test.php", data);
});

server side you receive the data

 $html = $_POST['html']

$.post() expects an object to be passed to transfert data along with the post request:

jQuery.post( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )

url: A string containing the URL to which the request is sent.
data: A map or string that is sent to the server with the request.
success(data, textStatus, jqXHR): A callback function that is executed if the request succeeds.
dataType: The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).

var content = $ ('body').html(),
    data = { content: content };

$.post('ajax/test.php', data);

Sending html in the post doesn't sound like a good idea. All elements type of input or select will be empty in the Html. You would need to use .serialize in order to get the values.

$('#submit').submit(function () {
    $.ajax({
       type: 'POST',
       url: 'ajax/test.php',
       data: {
           html: $('body').html()
       }
    });        
});

本文标签: javascriptVery simple jquery post() (trying to send html to another page)Stack Overflow