如何在ASP.NET文件上发布JQUERY帖子
大家下午好,我想发一篇帖子,我已经读了一些论坛,我明白了,但它不起作用 这是我的html文件如何在ASP.NET文件上发布JQUERY帖子,jquery,asp.net,post,jquery-post,Jquery,Asp.net,Post,Jquery Post,大家下午好,我想发一篇帖子,我已经读了一些论坛,我明白了,但它不起作用 这是我的html文件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <tit
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
</head>
<script type="text/javascript">
function run_javascript_function() {
$.ajax({
type: "POST",
url: "testclass.aspx/getdata",
//contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.d);
},
error: function () { alert("Ajax Error"); }
});
alert("Data from JavaScript");
}
</script>
<body>
<input type="button" value="Run Jquery or Javascript Function" onclick="run_javascript_function()" />
</body>
</html>
函数运行\u javascript\u函数(){
$.ajax({
类型:“POST”,
url:“testclass.aspx/getdata”,
//contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
警报(数据d);
},
错误:函数(){alert(“Ajax错误”);}
});
警报(“来自JavaScript的数据”);
}
这是testclass.aspx文件
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="testclass.aspx.vb" Inherits="testclass" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
这是代码文件
Imports System.Web.Services
Imports System.Web.Script.Services
Partial Class testclass
Inherits System.Web.UI.Page
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function getdata() As String
Return "some data from post"
End Function
End Class
导入System.Web.Services
导入System.Web.Script.Services
部分类testclass
继承System.Web.UI.Page
_
_
作为字符串的公共共享函数getdata()
返回“来自post的一些数据”
端函数
末级
当我运行这个脚本时,我得到的是“Ajax错误”,而不是“来自帖子的一些数据”,你知道如何做到这一点吗?如果我在webconfig或其他东西上遗漏了一些东西?你的
运行javascript函数()
函数及其
标记位于
和
之间的任何地方。只需将其移动到标签内即可:
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function run_javascript_function() {
$.ajax({
type: "POST",
url: "testclass.aspx/getdata",
//contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.d);
},
error: function () { alert("Ajax Error"); }
});
alert("Data from JavaScript");
}
</script>
</head>
函数运行\u javascript\u函数(){
$.ajax({
类型:“POST”,
url:“testclass.aspx/getdata”,
//contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
警报(数据d);
},
错误:函数(){alert(“Ajax错误”);}
});
警报(“来自JavaScript的数据”);
}
结果如下:
我已经修复了它,所以我在
标记中添加了脚本,但我必须在我的脚本上启用行contentType:“application/json;charset=utf-8”
,它工作得很好!,我去年使用PHP,我不需要在head或body标签上添加脚本,它们无论如何都可以工作,但这里有点不同,但没关系,谢谢@afzalulh您好,谢谢您的回复,我尝试过您提到我将脚本放在标签中,但仍然是Ajax错误,我检查了firebug上是什么让我这么做的,我得到了这个
嘿,老兄,我已经修复了它,所以我在
标记中添加了脚本,但我必须在我的脚本上启用contentType:“application/json;charset=utf-8”
,它工作得很好!,我去年使用PHP,我不需要在head或body标签上添加脚本,它们无论如何都可以工作,但这里有点不同,但没关系,谢谢兄弟!