使用jQuery AJAX传递数据 [英] Passing data with jquery ajax

查看:158
本文介绍了使用jQuery AJAX传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是得到错误警报,我想不出有什么不对。我只是想找回字符串(TESTEX pression),我送。它必须是一些与数据部分,因为没有它的工作原理参数。

I always get the 'error' alert, and I can't figure out what's wrong. I'm just trying to get back the string ("testexpression") that I send. It has to be something with the data part, because without a parameter it works.

下面是jQuery的一部分:

Here's the jquery part:

<script>

$("#meaning").blur(function () {

    $.ajax({ 
        type: "POST",
        url: '/GetMeaning/',
        data: {"expression" : "testexpression"},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: successFunc,
        error: errorFunc
    });

    function successFunc(data, status) {
        $("#dictionaryDropDown").html(data);
    }

    function errorFunc() {
        alert('error');
    }
})
</script>

这是控制器:

And this is the controller:

    public class GetMeaningController : Controller
{
    //
    // GET: /GetMeaning/

    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(string expression)
    {

        return Json(expression, JsonRequestBehavior.AllowGet);

    }

}

(更新:该类型后,我只是把它用得为好,而我在离开它)

(update: the type is post, I was just trying it out with get as well, and I left it in)

推荐答案

您需要发送的数据为字符串/ JSON。您正在发送的JavaScript对象。此外,该URL可能需要一个绝对URL,而不是相对URL

You need to send data as a string/json. You are sending a javascript object. Also, The URL might need to be a absolute url and not a relative url

$("#meaning").blur(function () {

    $.ajax({ 
        type: "POST",
        url: '/GetMeaning/',
        data: JSON.stringify({expression: "testexpression"}),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: successFunc,
        error: errorFunc
    });

    function successFunc(data, status) {
        $("#dictionaryDropDown").html(data);
    }

    function errorFunc() {
        alert('error');
    }
})

这篇关于使用jQuery AJAX传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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