从 jQuery.get() 返回 responseText [英] return responseText from jQuery.get()

查看:28
本文介绍了从 jQuery.get() 返回 responseText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做这样的事情:

var msg = $.get("my_script.php");

我认为 msg 会被设置为 my_script.php 返回的文本,即jqXHR 对象的 responseText.它显然不是这样工作的,因为 msg 总是设置为[object XMLHttpRequest]"

I thought msg would be set to the text returned by my_script.php,i.e. the responseText of the jqXHR object. It apparently doesn't work like that as msg is always set to "[object XMLHttpRequest]"

有没有一种快速的 1 行方式来做我想做的事?

Is there a quick 1 line way to do what I want?

谢谢.

推荐答案

经过一些测试,我最终找到了解决方案.

After some testing, I ended up finding a solution.

我需要同步调用,$.get 速记函数总是异步的,所以我需要使用 $.ajax,像这样:

I need the call to be synchronous, $.get shorthand function is always asynchonous, so I will need to use $.ajax, like this:

var msg = $.ajax({type: "GET", url: "my_script.php", async: false}).responseText;

我认为没有更好的方法可以做到这一点,感谢您的回答.

I don't think there is a better way to do this, thanks for your answers.

这篇关于从 jQuery.get() 返回 responseText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆