无法通过参数获取ajax调用以达到我的C#功能

时间:2018-05-03 13:43:23

标签: c# jquery asp.net ajax webmethod

的Ajax:

var test = "test";

$.ajax(
{
  type: "POST",
  url: "project/function",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  data: { input: test },
  success: function (response) {
    $("#lblMsg").text(response.d);
  },
  failure: function (response) {
    alert(response.d);
  }
});

C#功能:

[WebMethod]
public void function(string input)
{
}

当我没有包含参数时,连接成功。我尝试了不同的单引号和双引号排列的数据' ajax呼叫的一部分,无济于事。

我也尝试将dataType设置为" text"结果相似。

我错过了什么?

2 个答案:

答案 0 :(得分:1)

我建议您不要将数据作为JSON发送。只需删除

即可
Await

并且jQuery会将数据序列化为正常的url编码格式数据格式,这正是WebMethod所期望的。

答案 1 :(得分:0)

尝试这个可能会解决您的问题

var test =“test”;

    # placeholder for context processors output
    self.update({})

    # empty dict for any new modifications
    # (so that context processors don't overwrite them)
    self.update({})

@contextmanager
def bind_template(self, template):
    if self.template is not None:
        raise RuntimeError("Context is already bound to a template")

    self.template = template
    # Set context processors according to the template engine's settings.
    processors = (template.engine.template_context_processors +
                  self._processors)
    updates = {}
    for processor in processors:
        updates.update(processor(self.request))
    self.dicts[self._processors_index] = updates

    try:
        yield
    finally:
        self.template = None
        # Unset context processors.
        self.dicts[self._processors_index] = {}

def new(self, values=None):
    new_context = super(RequestContext, self).new(values)
    # This is for backwards-compatibility: RequestContexts created via
    # Context.new don't include values from context processors.
    if hasattr(new_context, '_processors_index'):
        del new_context._processors_index
    return new_context