【发布时间】:2016-08-09 15:22:00
【问题描述】:
我正在使用JQuery Ajax 将数据从一个域发送到另一个域中的ASP.NET 页面。我能够从ASP.NET 页面获得响应,但我无法从JQuery Ajax 发送数据。这是我的代码:
$.ajax({
url: 'http://somewebsite/dbd/leap.aspx',
data: '{ year: "' + $('#txtYear').val() + '"}',
type: 'POST',
async: true,
cache: false,
dataType: 'jsonp',
crossDomain: true,
contentType:"application/json; charset=utf-8",
success: function (result) {
console.log("inside sucess");
console.log("result: " + result);
},
error: function (request, error) {
// This callback function will trigger on unsuccessful action
alert('Error!');
}
});
我可以到达leap.aspx 页面。注意http://somewebsite 只是一个占位符。
leap.aspx 有以下内容:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="leap.aspx.cs" Inherits="_Default" %>
<%=output %>
leap.aspx.cs 有以下内容:注意我使用Request.Params["year"] 来获取Ajax 调用传递的年份值,但没有运气。
using System;
public partial class _Default : System.Web.UI.Page
{
public string output = "";
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["callback"] != null)
{
output = Request.Params["callback"] + "('" + GetData(Request.Params["year"]) + "')";
}
}
[System.Web.Services.WebMethod]
public string GetData(string year)
{
return "From outer space! " + year;
}
}
当我运行这段代码时,在控制台中我看到了
“结果:来自外太空!”
但我没有看到Ajax 发送的年度数据。我正在使用JSONP,因为我正在发出跨域请求以绕过同源策略。我没有收到任何错误。我只是没有得到从ASP.NET 页面返回的年份值。
我如何获得年份?
【问题讨论】:
-
如果数据是 {year: "2001"} 我认为在很多情况下,您希望 .json 在键和值周围加上引号...确实会更改 @ 的格式987654337@ 到 {"year": "2001"} 修复它?
标签: javascript c# jquery asp.net ajax