2015-02-09 57 views
0

login.aspx.csAsp.net邮政JSON

[System.Web.Services.WebMethod] 
     public static string jQueryPageMethod(string name) 
     { 
      return "<h3>jQuery - PageMethod </h3>result" + name; 
     } 

JS/jQuery的:

如果我运行下面的方法,它的工作原理。

$.ajax({ 
    type: 'POST', 
    url: 'login.aspx/jQueryPageMethod', 
    data: '{ "name":"exampleValue" }', 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    success: function(result) { 
     alert("success"); 
    }, 
    error: function() { 
     alert('error'); 
    } 
}); 

如果我运行下面的方法它不工作

var somevalue = "Value"; 

$.ajax({ 
    type: 'POST', 
    url: 'login.aspx/jQueryPageMethod', 
    data: '{ "name":' + somevalue + ' }', // Problem here 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    success: function(result) { 
    alert("success"); 
    }, 
    error: function() { 
    alert('error'); 
    } 
}); 

其中i在第二示例错过数据的一部分?

+0

你能否更详细地解释什么是你正在尝试做? – Dana 2015-02-09 13:58:30

+0

你能提供你所得到的错误吗? – 2015-02-09 13:59:18

+0

我想发布值为jQueryPageMethod命名方法 – Soner 2015-02-09 14:04:11

回答

0

试试这个data: '{"queryname":"' + queryname + '"}'

+0

这个作品....... :) – Soner 2015-02-09 14:08:21

+0

这很奇怪。不知何故,在发布http请求之前,你都对数据做了些什么。你提到了json stringify - 它将把一个字符串转换成json对象,但这就像过河(两次)到水中一样... – 2015-02-09 14:20:48

+0

我不是任何东西。只是传递给定的文本变量。 – causita 2015-02-09 14:24:39

1

你的数据不应该被格式化为一个字符串,而是作为一个JavaScript对象是这样的:

data: { "name": somevalue }, // No Problem here :) 
+0

仍然是同样的问题。由于字符串jsonstringfiy可能会发生问题? – Soner 2015-02-09 14:03:55

+0

@Soner我认为你不需要那个。你在哪里使用 - 我看不出你的例子有用吗?它看起来很直接。我的代码应该工作,除非发布的内容比发布的内容多? – 2015-02-09 14:04:57