jQuery的不的getJSON传递任何值控制器 [英] jquery getJson not passing any values to controller

查看:109
本文介绍了jQuery的不的getJSON传递任何值控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个文本框一些文字传递给控制器​​来获取JSON结果,像这样

I am trying to pass some text from a textbox to a controller to get JSON results like so

function invokeAction() {
        var searchText = $("#SearchTextBox").val();

        // Invoke MVC controller action
        $.getJSON("/Home/Results/" + searchText, bindResults);
    }

如果我在这里放一个警告,我可以看到SEARCHTEXT肯定有一个值,但是当我把一个破发点这个控制器动作:

If I put an alert here I can see that searchText definately has a value, but when I put a break point on this controller action:

 public ActionResult Results(string search)
    {
        var r = from t in db.Restaurants
                where SqlMethods.Like(t.Name, "%" + search + "%") || SqlMethods.Like(t.Postcode, search + "%") || SqlMethods.Like(t.CuisineType.Type, search + "%")
                orderby t.Name ascending
                orderby t.Rating descending
                orderby t.NumOfViews
                    descending
                select t;

        return Json(r.ToList());
    }

在传递的字符串为空,但当我在调试器检查HTTP上下文我SEARCHTEXT是URL的一部分。

The string passed in is null, yet when I check the http context in the debugger my searchtext is a part of the url.

由于这是空查询不返回任何结果。

As this is null the query returns no results.

我失去了一些东西在这里?

Am I missing something here?

推荐答案

我有一些问题,从服务返回的JSON和我没有得到任何电话回来。事实证明,JSON的格式不正确,我才得以测试,并通过处理普通的ajax调用的错误选项得到这些错误。

I've had some problems returning json from services and I wasn't getting any calls back. it turned out that the json was malformed and I was able to test that and get those errors by handling the error option of the plain ajax call.

$.ajax({
  type: "GET",
  url: "Home/Results/",
  data: { search: searchText },
  dataType: "json",
  error: function(xhr, status, error) {
    // you may need to handle me if the json is invalid
    // this is the ajax object
  },
  success: function(json){
    alert( "Data Returned: " + json);
  }
});

这篇关于jQuery的不的getJSON传递任何值控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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