将MVC Ajax转换为Jquery

I am in the process of learning how to convert MVC Ajax to jquery ajax so I can do more.

This is the old ajax, I took out the loading stuff

@Ajax.ActionLink("Update Tweets", "Index", "Home",
    new AjaxOptions
    {
        UpdateTargetId = "TweetBox",
        InsertionMode = InsertionMode.InsertBefore,
        HttpMethod = "Get",
    })

I need to convert this to jquery ajax. It seems to be working lets see the code

<script>
    $(document).ready(function () {
        $("#StartLabel").click(function (e) {

            $.ajax({
                type: "Get",
                url: '/Home/Index',
                //  data: "X-Requested-With=XMLHttpRequest",
                //  contentType: "application/text; charset=utf-8",
                dataType: "text",
                async: true,
                //    cache: false,
                success: function (data) {
                    $('#TweetBox').prepend(data);
                    alert('Load was performed.');
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                },
                complete: function (resp) {
                    alert(resp.getAllResponseHeaders());
                }
            });
        });
    });
</script>

In the microsoft ajax it sets XML Request in the headers. Do I need to add that too? I am just paging my controller that performs a query to twitter and appends the data to the top.

I am using fiddler to see how the requests are different but the results are the same.

I also noticed if i put the text in the data: object its puts it in the header. i dont think that is right by any means.

You could define a normal anchor:

@Html.ActionLink("Update Tweets", "Index", "Home", null, new { id = "mylink" })

And then unobtrusively AJAXify it:

$(document).ready(function () {
    $("#mylink").click(function (e) {
        $.ajax({
            type: "GET",
            url: this.href,
            success: function (data) {
                $('#TweetBox').prepend(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            },
            complete: function (resp) {
                alert(resp.getAllResponseHeaders());
            }
        });

        return false;
    });
});

Notice that I return false from the click handler in order to cancel the default action. Also notice that I am using the anchor's href property instead of hardcoding it.

The 2 AJAX requests should be identical.

Here is simple example using Ajax with Jason data

// Post JSON data
    [HttpPost]
    public JsonResult JsonFullName(string fname, string lastname)
    {
        var data = "{ \"fname\" : \"" + fname  + " \" , \"lastname\" : \"" + lastname + "\" }";
        return Json(data, JsonRequestBehavior.AllowGet);  
    }

in the view add a reference to the query as following @section Scripts{

<script src="~/Scripts/modernizr-2.6.2.js"></script>
<script src="~/Scripts/jquery-1.8.2.intellisense.js"></script>
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>  

}

in the view add the js note: (jsonGetfullname).on is a button

  <script type="text/javascript"> 
    $("#jsonGetfullname").on("click", function () { 
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "@(Url.Action("JsonFullName", "Home"))",
            data: "{ \"fname\" : \"modey\" , \"lastname\" : \"sayed\" }",
            dataType: "json",
            success: function (data) {
                var res = $.parseJSON(data);
                $("#myform").html("<h3>Json data: <h3>" + res.fname + ", " + res.lastname)
            }, 
            error: function (xhr, err) {
                alert("readyState: " + xhr.readyState + "
status: " + xhr.status);
                alert("responseText: " + xhr.responseText);
            } 
        })
    });
</script>

you can also use (POST\GET) as following:

[HttpPost]
    public string Contact(string message)
    { 
        return "<h1>Hi,</h1>we got your message, <br />" + message + " <br />Thanks a lot";
    }

in the view add the js note: (send).on is a button

  $("#send").on("click", function () {
        $.post('', { message: $('#msg').val() })
        .done(function (response) {
            setTimeout(function () { $("#myform").html(response) }, 2000);
        })
        .error(function () { alert('Error') })
        .success(function () { alert('OK') })
        return false;
    });