开发者

JQUERY GET operation not working

开发者 https://www.devze.com 2023-01-24 22:35 出处:网络
I\'m having trouble with th开发者_StackOverflowe following JQuery script $(\'#extra_data\').append(\'<div id=\"tabs-\' + (tab_length + 1) + \'\"></div>\');

I'm having trouble with th开发者_StackOverflowe following JQuery script

$('#extra_data').append('<div id="tabs-' + (tab_length + 1) + '"></div>');
    $.get(url, function(data) {
    $('#tabs-' + (tab_length + 1)).html(data);
     }); 

My trouble is that the $.get(..) operation doesn't return any results - although when using firebug it shows the ajax call as expected.

Any clues?

Thanks.


Controller

<HttpPost()> _
Function GetPartialView() As ActionResult
    If (Request.IsAjaxRequest()) Then
        Return View("PVTest")
    Else
        Return View()
    End If
End Function

I've filtered the request if it is Ajax. You can even pass an object to your partial view.

jQuery

<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            type: 'POST',
            url: 'Home/GetPartialView',
            data: {},
            dataType: 'json',
            beforeSend: function(XMLHttpRequest) {
            },
            complete: function(XMLHttpRequest, textStatus) {
                $('#extra_data').append(XMLHttpRequest.responseText);
            }
        });
    });
</script>

Partial View (PVTest.ascx)

<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl" %>
<div id="01">
    Hello World
</div>


Try load method:
$('#extra_data').append('');
$('#tabs-' + (tab_length + 1)).load(url)


I think you need to use Post and [HttpPost] in ASP.NET MVC, I think there is a security issue related to GET.

I only seem to use Post operations and remember seeing something about security. Will see if I can verify that...

ADDED:

see: ASP.NET MVC 2.0 JsonRequestBehavior Global Setting


I would use a POST, as Mark suggested:

$.ajax({
    type: 'POST',
    url: url,
    data: { },
    dataType: 'json',
    beforeSend: function(XMLHttpRequest) {
    },
    complete: function(XMLHttpRequest, textStatus) {
    var Response = $.parseJSON(XMLHttpRequest.responseText);
    }
});

the Response should contain the JSON stream. You can append it to your element.

The controller should do something like this:

<HttpPost()> _
Function DoSomething() As ActionResult

    Return (Json(myObject, JsonRequestBehavior.DenyGet))

End Function
0

精彩评论

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