$getjson 和布尔值
我正在学习jquery。我有一个 url,它给出了 json 转储“/check_email_exists”,给出了 true 或 false。这主要是 jsondump 的 HttpResponse。
如何编写一个 Javascript 函数,根据 json true 或 false 给出 true 或 false。
我尝试过,
function ifEmailExists(email)
{
return $.getJSON("/check_email_exists/" + email);
}
显然这是行不通的。正确的方法是什么?
编辑
我也尝试过但
function ifEmailExists(email)
{
$.getJSON("/check_email_exists/" + email), function(data)
{
return data;
}
};
没有成功
I am learning jquery. I have a url which gives a json dump "/check_email_exists" which gives true or false. This is primarily HttpResponse of a jsondump.
How do I write a Javascript function which gives a true or false based on the json true or false.
I tried this
function ifEmailExists(email)
{
return $.getJSON("/check_email_exists/" + email);
}
Obviously this is not working.What's the right way?
edit
I also tried this
function ifEmailExists(email)
{
$.getJSON("/check_email_exists/" + email), function(data)
{
return data;
}
};
No Success
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自
getJSON
文档:AJAX 是异步的(即第一个 A)。这意味着结果不会立即返回。如果您想对
getJSON
返回的数据执行某些操作,则需要使用另一个参数,这是一个 success 函数。当返回对请求的响应时,将触发此函数。您无法从函数返回此响应,因为在函数完成执行时它尚未进入。如果您绝对必须阻塞并等待响应,则可以使用
$.ajax
而不是getJSON
并将async
参数设置为 false。这将迫使呼叫等待响应。这通常是一个坏主意,因此您可能需要考虑是否有某种方法可以使用异步行为获得相同的功能。使用async: false
会在等待响应时挂起浏览器。如果这个响应需要一段时间才能回来,大多数情况下这将是非常糟糕的用户体验。From the
getJSON
doc:AJAX is asynchronous (that's the first A). This means that the result is not returned immediately. If you want to do something with the returned data from
getJSON
you will need to use the other parameter, which is a success function. This function fires when the response to the request is returned. You cannot return this response from your function as it has not come in yet at the time your function completes execution.If you absolutely have to block and wait for the response, you can use
$.ajax
instead ofgetJSON
and set theasync
parameter to false. This will force the call to wait for a response. This is generally a bad idea so you probably want to consider if there's some way for you to get the same functionality using the asynchronous behavior. Usingasync: false
will hang the browser while it waits for the response. If this response takes some time to come back, it will be a very bad user experience in most cases.当数据只检索布尔值时,您应该能够像这样获取它。 这应该提醒 True 或 False。
如果它提醒类似
[object Object]
的内容,那么它会检索带有字段名称的对象,在这种情况下您可以使用此alert(data.exists)
,其中data
是对象,exists
是该对象的一个字段。When the data only retrieves boolean value, you should be able to get it like this. This should alert True or False.
If it alerts something like this
[object Object]
, then it retrievs a object with field names, in that case you can use thisalert(data.exists)
, wheredata
is object andexists
is one field of that object.您可以创建一个像这样的函数,返回 true 或 false
确保
http://localhost:8080/yourapp/check_email_exists
返回 true 或 falseYou can make a function like this that returns true or false to you
Make sure
http://localhost:8080/yourapp/check_email_exists
returns true or false