通过 jquery 返回字符串的同步 ajax 调用

发布于 2024-08-14 07:03:58 字数 346 浏览 3 评论 0原文

我正在对 ajax 进行 jQuery 同步调用——返回类型设置为“json”——但是返回数据以字符串形式返回。我做错了什么,或者是否可以将字符串转换为对象?

var ajax_response = $.ajax({url:"ajx_services", async:false, type:"post", data:{method: "getItem", item_no: $(ctx).val()}, dataType:"json"});
var data = ajax_response.responseText;

ajax 调用正在工作,正如我可以在调试器中看到结果一样,只是返回的数据位于字符串中。

I am making a jQuery synchronous call to ajax -- with the return type set to "json" -- however the return data is coming back as a string. Is there something I'm doing wrong, or is there away to convert the string to an object?

var ajax_response = $.ajax({url:"ajx_services", async:false, type:"post", data:{method: "getItem", item_no: $(ctx).val()}, dataType:"json"});
var data = ajax_response.responseText;

The ajax call is working, as I can see the results in the debugger, it is simply that the returned data is in a string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木槿暧夏七纪年 2024-08-21 07:03:58

responseText 将始终是一个字符串。在 $.ajax() 中添加一个带有参数的“成功”回调函数,该函数将是 JSON 数据。

$.ajax({
    -- other stuff --
  , success: function(data)
    {
        // do something with data
    }
});

responseText will always be a string. within $.ajax() add a 'success' callback function with a parameter and that will be the JSON data.

$.ajax({
    -- other stuff --
  , success: function(data)
    {
        // do something with data
    }
});
伴随着你 2024-08-21 07:03:58

这是因为 $.ajax() 将返回一个实际的 XMLHttpRequest ..其 responseText 没有 JSON 等概念。

试试这个:

var data = (function () {
    var ajaxResponse = '';
    $.ajax({url:"ajx_services", async:false, type:"post", data:{method: "getItem", item_no: $(ctx).val()}, success: function (data) {
        ajaxResponse = data;
    }, dataType:"json"}); 
    return ajaxResponse;
}());

因为 ajaxResponse 是在父闭包中定义的在 ajax() 调用中定义的函数,它可以设置为 success 函数提供的 JSON data 对象(即当 Ajax 请求成功完成时调用)。然后,父闭包返回ajaxResponse,然后将其分配给外部data变量。

请注意,这种从 ajax() 立即返回修改后的 ajaxResponse 的能力只有在请求是同步时才可能实现。如果是异步的,return ajaxResponse 很可能会返回一个空字符串。

That is because $.ajax() will return an actual XMLHttpRequest .. whose responseText has no concept of JSON etc.

Try this:

var data = (function () {
    var ajaxResponse = '';
    $.ajax({url:"ajx_services", async:false, type:"post", data:{method: "getItem", item_no: $(ctx).val()}, success: function (data) {
        ajaxResponse = data;
    }, dataType:"json"}); 
    return ajaxResponse;
}());

Since ajaxResponse is defined in the parent closure of the function defined in the ajax() call, it can be set to the JSON data object that is provided by the success function (which is called when the Ajax request successfully completes). Then, ajaxResponse is returned by the parent closure, which is then assigned to the outside data variable.

Note that this ability to immediately return the modified ajaxResponse from the ajax() is only possible because the request is synchronous. If it were async, return ajaxResponse would very likely return an empty string.

诺曦 2024-08-21 07:03:58

这种方式应该可行,使用 eval 函数:

var ajax_response = $.ajax({url:"ajx_services", async:false, type:"post", data:{method: "getItem", item_no: $(ctx).val()}, dataType:"json"});
ajax_response.onreadystatechange = xHandler;

function xHandler() {
  if (ajax_response.readyState == 4) {
    var data = eval('(' + ajax_response.responseText + ')');
  }
}

但正如之前所说,您应该使用 jQuery 成功回调,它的存​​在是为了让您的生活更轻松。

That way should work, using the eval function:

var ajax_response = $.ajax({url:"ajx_services", async:false, type:"post", data:{method: "getItem", item_no: $(ctx).val()}, dataType:"json"});
ajax_response.onreadystatechange = xHandler;

function xHandler() {
  if (ajax_response.readyState == 4) {
    var data = eval('(' + ajax_response.responseText + ')');
  }
}

But as it has been said before, you should use jQuery success callback, which exists to make your life easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文