使用$阿贾克斯或$。员额致电MVC 5控制器的方法 [英] Using $.ajax or $.post to call MVC 5 Controller method

查看:149
本文介绍了使用$阿贾克斯或$。员额致电MVC 5控制器的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置什么应该是从一个MVC页面非常简单的调用使用JavaScript控制器。这是我的控制器:

I'm trying to set up what should be a very simple call from an MVC page to a controller using JavaScript. This is my Controller:

Imports System.Web.Mvc

Namespace Controllers
    Public Class DataController
        Inherits Controller

        Function Index() As ActionResult
            Return View()
        End Function

        <HttpPost>
        Function SaveData(payload As String) As String
            If payload IsNot Nothing AndAlso payload.Length > 0 Then
                Return "Good"
            Else
                Return "Bad"
            End If
        End Function

    End Class
End Namespace

这是我的看法(Index.vbhtml):

this is my View (Index.vbhtml):

@Code
    ViewData("Title") = "Index"
End Code

<h2>Index</h2>

@Code
    Html.BeginForm()
    @:<a href="#" onclick="SaveData();">Save Data</a>
    Html.EndForm()
End Code

这是我的JavaScript(通过_layout.vbhtml文件包括):

and this is my JavaScript (included via the _layout.vbhtml file):

function SaveData() {

    var payload = "TEST_DATA_GOES_HERE";

    // Calls controller correctly but data is null
    $.ajax({
        url: "/Data/SaveData",
        type: "POST",
        processData: false,
        dataType: String,
        data: { payload: payload }
    })
    .done(function () { alert('Application saved.'); })
    .fail(function () { alert('Application failed to save.'); });

    // Returns a 500 error
    $.post("/Data/SaveData", { Payload: payload }, function (data) { alert('Application saved. ' + data); }, "String");

    // Calls controller correctly but data is null
    $.post("/Data/SaveData", payload, function () { alert('Application saved.' + data); }, "String");
}

一切调试,但负载变量到达的没有当连接了很好保存数据功能。我已经尝试过使用 $。交,但类似的问题,我所发现使用这两种方法的引用简单地认为它会工作,第一次没有错误,不要吨有任何故障排除技巧。

Everything connects up nicely when debugging but the payload variable arrives as Nothing in the SaveData function. I've tried using $.post but with similar problems and all the references I've found to using either method simply assume that it will work first time without error and don't have any troubleshooting tips.

如何配置的 $。阿贾克斯 $。交函数正确调用控制器,并通过一简单的字符串?

How do you configure the $.ajax or $.post functions to correctly call a controller and pass a simple string?

推荐答案

您需要设置过程数据作为要发送的对象,这将需要被转换成一个查询字符串。

You need to set processData to true as you are sending an object which will need to be converted to a querystring.

从API:

过程数据
  默认情况下,该数据选择通过为对象(从技术上讲,任何String之外)将被处理并转换成一个查询字符串数据,拟合到默认的内容类型应用程序/ x-WWW的形式urlen codeD。如果您想发送一个DOM文档,或其他非处理的数据,将此选项设置为false。

ProcessData
By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

http://api.jquery.com/jQuery.ajax/

这篇关于使用$阿贾克斯或$。员额致电MVC 5控制器的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆