Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get partial HTML with AJAX

Tags:

jquery

I am trying to get a content of a another page using AJAX call. I am able to get the entire document but is it possible to get a better precision and get only a section that is within a target div?

<div class="myTarget">content that I need</div> 

Here's how I get it now:

$.ajax({
    type: "GET",
    url: "/dir/targetPage",
    dataType: "html",
    success: function(data)
    {
      $('#dropHere').html(data);
    }
}); 
like image 770
santa Avatar asked Jan 07 '13 18:01

santa


People also ask

How to return partial view from AJAX call?

When making AJAX requests, it is very simple to return HTML content as the result. Simply return an ActionResult using the PartialView method that will return rendered HTML to the calling JavaScript. Now define an action method in the book controller that returns an ActionResult using the PartialView.

How do I render partial view?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

How do I call ActionResult from AJAX?

1st you need to return a partial view. You need to be aware of ajax parameters contentType and dataType. From the documentation: contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8'). This specifies what type of data you're sending to the server.

What is a partial HTML?

A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.


1 Answers

$('#dropHere').load('/dir/targetPage #ele');

Where '#ele' is an element with the id of ele and all it's inner children.

like image 105
Ohgodwhy Avatar answered Sep 28 '22 07:09

Ohgodwhy