[英]Can't Get jquery AJAX request to process
Hi I'm trying to do just a simple ajax call to a method in my code behind. 嗨,我正在尝试对我后面的代码中的方法进行简单的ajax调用。 At this point I'm just testing, so I'm keeping it as simple as possible, but it's always erroring.
在这一点上,我只是在测试,所以我将其保持尽可能的简单,但始终会出错。 I'm sure it's just a simple mistake, but I have no idea what to do.
我敢肯定这只是一个简单的错误,但我不知道该怎么办。 It seems really straight forward, and it should work, but when I click the button, it just pops up an alert saying "[object Object]".
看起来确实很简单,并且应该可以使用,但是当我单击按钮时,它会弹出一个警告,提示“ [object Object]”。 Here is my code, any help is greatly appreciated:
这是我的代码,非常感谢您的帮助:
aspx file: aspx文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="Program.TestPage" %>
<!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>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="btnSubmit" value="Submit" />
</div>
</form>
<script type="text/javascript">
$(function () {
$('#btnSubmit').click(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "TestPage.aspx/InsertData",
dataType: "json",
data: "{}",
success: function (data) {
alert(data.d);
},
error: function (result) {
alert(result);
}
});
});
});
</script>
</body>
</html>
Code Behind: 背后的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Web.Services;
namespace Program
{
public partial class TestPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string InsertData()
{
string retMessage = "ddd";
return retMessage;
}
}
}
UPDATE UPDATE
Sorry everyone, turns out the issue was in my global.asax. 抱歉,每个人都发现问题出在我的global.asax中。 It was intercepting the ajax request, and messing everything up.
它正在拦截ajax请求,并弄乱了所有内容。 Once I made sure the HTTP was a GET in the asax file, it stopped killing my ajax request, and started working.
一旦确定HTTP是asax文件中的GET,它就停止杀死我的ajax请求,并开始工作。
In your javascript, you're calling InsertData
as a POST
在您的JavaScript中,您将
POST
称为InsertData
Try 尝试
[WebMethod]
[HttpPost]
public static JsonResult InsertData()
{
return Json(new { Message = "ddd" }, JsonRequestBehavior.AllowGet);
}
And in your javascript, 在您的JavaScript中,
success: function (data) {
alert(data.Message);
},
You can try this: 您可以尝试以下方法:
var dataToPost = "{ country:'" + 2 + "', amount:" + 4.02 + "}";
$.ajax({
url: @Url.Action("PostAmount", "Deal")',
type: "POST",
dataType: 'json',
data: dataToPost,
cache: false,
contentType: "application/jsonrequest; charset=utf-8",
success: function (data) {
alert("hi"+ data);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.