[英]Error while using Json on Asp
I'm using JavaScript and C# on aspnet. 我在ASPNET上使用JavaScript和C#。 And i want to pass 3 values from the Asp Page to the code behind, and to do so i am using Json method.
我想将3个值从Asp页面传递到后面的代码,并且这样做是使用Json方法。 Here it is how i do:
这是我的方法:
//initialize x, y and nome
var requestParameter = { 'xx': x, 'yy': y, 'name': nome };
$.ajax({
type: 'POST',
url: 'Canvas.aspx/GetData',
data: requestParameter,
//contentType: "plain/text",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
alert(data.x);
},
error: function () { alert("error"); }
});
And then on the C# i do: 然后在C#上执行:
[WebMethod]
public static string GetData(Object[] output)
{
return output.ToString();
}
For some reason i keep on getting the alert saying "error" (the one that i defined on the ajax post method). 由于某种原因,我不断收到警告说“错误”(我在ajax post方法上定义的警告)。 I would like to know why, and how can i avoid that.
我想知道为什么,以及如何避免这种情况。 Thanks in advance
提前致谢
The 该
{ 'xx': x, 'yy': y, 'name': nome }
Is not valid json. 无效的json。
A valid is 有效为
var requestParameter = { "xx": 1, "yy": 11, "name": "test" }
In order to run just change the parameter on webmethod
and from object[]
to Dictionary<string,object>
为了运行,只需更改
webmethod
上的参数并将其从object[]
更改为Dictionary<string,object>
As continue of your last comment i update my post with one more solution. 在您最后发表评论时,我将用另一种解决方案更新我的帖子。
Aspx page Aspx页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
function testmethod()
{
var requestParameter = { "xx": 1, "yy": 11, "name": "adadsaasd111" };
PageMethods.test(requestParameter);
}
function test()
{
testmethod();
}
</script>
<input id="Button1" type="button" onclick="return test();" value="button" />
</form>
cs code CS代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod]
public static void test(Dictionary<string,object> cal)
{
// todo
}
}
} }
change var requestParameter = { 'xx': x, 'yy': y, 'name': nome };
更改
var requestParameter = { 'xx': x, 'yy': y, 'name': nome };
to 至
var requestParameter = { "xx": "'+x+'", "yy": "'+y+'", "name": "'+nome+'" };
also add 还添加
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
after [WebMethod]
在
[WebMethod]
also before the declaration of class add 也在类声明之前添加
[System.Web.Script.Services.ScriptService]
this tag is required to allow web methods to be called from scripts 此标记是必需的,以允许从脚本调用Web方法
Your webservice should be like this 您的网络服务应该是这样的
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetData(String xx, String yy, String name)
{
return xx+yy+name;
}
and jquery 和jQuery
$.ajax({
url: '/Canvas.aspx/GetData',// the path should be correct
data: '{ "xx": "'+x+'", "yy": "'+y+'", "name": "'+nome+'" }',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'POST',
success: function (msg) {
alert(msg.d);
},
error: function (msg) {
//alert(msg.d);
alert('error');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.