gpt4 book ai didi

javascript - Asp.net MVC jQuery Ajax 调用 JsonResult 不返回任何数据

转载 作者:行者123 更新时间:2023-11-30 18:55:06 25 4
gpt4 key购买 nike

我在页面上加载了这个脚本:

(function() {
window.alert('bookmarklet started');
function AjaxSuccess(data, textStatus, xmlHttpRequest) {
if (typeof (data) == 'undefined') {
return alert('Data is undefined');
}
alert('ajax success' + (data || ': no data'));
}
function AjaxError(xmlHttpRequest, textStatus, errorThrown) {
alert('ajax failure:' + textStatus);
}
/*imaginarydevelopment.com/Sfc*/
var destination = { url: 'http://localhost:3041/Bookmarklet/SaveHtml', type: 'POST', success: AjaxSuccess, error: AjaxError,
dataType: 'text',contentType: 'application/x-www-form-urlencoded'
};
if (typeof (jQuery) == 'undefined') {
return alert('jQuery not defined');
}

if (typeof ($jq) == 'undefined') {
if (typeof ($) != 'undefined') {
$jq = $;
} else {
return alert('$jq->jquerify not defined');
}
}
if ($jq('body').length <= 0) {
return alert('Could not query body length');
}
if ($jq('head title:contains(BookmarkletTest)').length > 0) {
alert('doing test');
destination.data = { data: 'BookmarkletTestAjax' };
$jq.ajax(destination);
return;
}

})();

当它在 VS2008 的 cassini 中本地运行时,ajax 成功显示从 Asp.net MVC 返回的字符串,当它在远程运行时,ajax 成功数据为空。这是在本地和远程运行时触发的 Controller 方法:

    [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
public string SaveHtml(string data)
{
var path = getPath(Server.MapPath);
System.IO.File.WriteAllText(path,data);
Console.WriteLine("SaveHtml called");
Debug.WriteLine("SaveHtml called");

//return Json(new { result = "SaveHtml Success" });
return "SaveHtml Success";
}

一旦它开始工作,我打算删除 GET,但目前直接从网络浏览器访问 SaveHtml 方法会在测试时产生预期的结果。

所以我认为我的 javascript 有问题,因为当我使用 chrome 的开发人员工具逐步执行时,我看到数据为空,而且 xmlHttpRequest 似乎也没有在任何地方获得预期的结果。

我正在通过加载 jqueryhttp://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js

最佳答案

当它“远程”运行时——在真实服务器上——域名是什么? Ajax 请求的目标 URL 是什么?您知道,这些域必须相同。您不能部署到 http://your.application.domain/foo然后向 http://some.other.domain/bar 发出 Ajax 请求因为安全限制。

编辑 对不起,我收回;今天早上咖啡不够:-)

关于javascript - Asp.net MVC jQuery Ajax 调用 JsonResult 不返回任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2392596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com