开发者

jQuery .ajax load function, how to pass in data and retrieve it?

I have something开发者_高级运维 like the following:

    $.ajax({
        url: "info.html?" + $(this).attr('id'),
        cache: false,
        success: function(html){
            $('#list-content').load("info.html?" + $(this).attr('id'));
        }
    })

In info.html, if I get the document.href and try to parse it, I don't get info.html?..., instead, I get the URL of the containing window which is index.html. Question, how do I get the data trailing the 'info.html?''? Is this good practice? How else can I pass in data and how can I retrieve it from info.html on document ready?

One more question, is there a way to access the elements inside info.html after loading it?

Thanks in advance.


i think this is more like something you want:

    $.ajax({
      url: "info.html",
      data: {
        id:$(this).attr('id')
      },
      cache: false,
      success: function(html){
          $('#list-content').html(data);
      }
    })

If I understand you correctly, you are currently on index.html and you want to grab info.html and put it in a div? If so, this is how you do it.

I'm not entirely sure why you are putting GET params on the url though. There is a data param in the ajax function that will automatically create your GET string, I put it in the example if that is what you want.


ANSWER TO BELOW QUESTION:

Well you'd want to use a server side language, php is a popular choice. So you might want to consider renaming your file to info.php (don't worry, it will still work if it's filled with html). Then you'll have access to the GET param in php, you can do something like:

<?php
$id=isset($_GET['id']) ? $_GET['id'] : null;
?>

From there you can do whatever you want with $id, echo it, use it in a db query, etc.


Thats because ajax request are handled by the server and you are still inside index.html, which is on the client side. You can access them via $_GET in php which is the default method for load. As for accessing data in info.html, you can easily do it in a callback function after load.

$('#list-content').load("info.html?" + $(this).attr('id'), function() {
     // js stuff here
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜