可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

I have a simple function which only returns a translated message from the server to the client. But the result shows undefined, when I pass the result into a var.

function MessageNoResult() {

$.ajax(

{

type: "POST",

async: true,

url: '',

contentType: "application/json; charset=utf-8",

dataType: "json",

success: function(msg) {

return msg.d;

}

});

}

Result --> Undefined (bad)

var message = MessageNoResult();

alert(message);

When I look into the Headers it gives me:

Server ASP.NET Development Server/9.0.0.0

Date Wed, 09 Nov 2011 09:01:31 GMT

X-AspNet-Version 2.0.50727

Cache-Control private, max-age=0

Content-Type application/json; charset=utf-8

Content-Length 24

Connection Close

Response {"d":"No search result"}

JSON d "No search result"

Why do I still get an undefined result?

回答1:

You can pass a callback function to the MessageNoResult function

function MessageNoResult(callback) {

$.ajax(

{

type: "POST",

async: true,

url: '',

contentType: "application/json; charset=utf-8",

dataType: "json",

success: function(msg) {

callback(msg);

}

});

}

And then call:

MessageNoResult(function(msg) {

alert(msg.d);

});

回答2:

The ajax call is still being executed as your code moves on to the alert(message) line, so the message variable has no value.

You need to pass the value of the response to a function within the success method.

success: function(msg) {

myFunction(msg.d);

}

// outside ajax call

function myFunction(ajaxValue) {

alert(ajaxValue)

}

回答3:

The ajax call is an async call and you’re running the alert right after the MessageNoResult, thus the message has no value until you’ve actually completed the ajax request.

You’ll need to run your alert inside the success from ajax call.

回答4:

You are trying to return from success which is a callback function. Set a global variable and use it within the success function retmess = msg.d to store the return or set the content of an html element jQuery('#status').html(msg);

回答5:

Your code:

var message = MessageNoResult();

is setting the message variable to the return value of the MessageNoResult functiion. Since you have no return value in this function, you get undefined as response.

The corrcet way to handle this is by dealing with the return value from the Ajax call inside the success callback.

For a similar question see my answer here:

ajax results,jQuery Ajax results in undefined相关推荐

  1. $.ajax data怎么处理_不会吧,不会吧,不愧是Ajax,jQuery Ajax啊

    Ajax and jQuery Ajax 我们做前端的,肯定不会只满足于写一些静态网页,我们希望做到网站(与服务器)的数据交互.不得不说,现在前端的发展真是太快了,不断的更新迭代,新的技术就如同雨后春 ...

  2. AJAX(三)jQuery AJAX.post

    在jQuery 中也提供了一个比较简单的ajax请求jQuery.post(url, [data], [callback], [type]),如果考虑出错时执行函数该方法不适合,请选择ajax 相对j ...

  3. JS Ajax 和 jQuery Ajax : 异步自动填充

    第四十八章 JS Ajax 和 jQuery Ajax : 异步自动填充 48.1 案例介绍 48.2 案例相关技术 48.2.1 JSON数据 48.2.2 JSON-LIB工具 48.3 案例分析 ...

  4. asmx 接受 ajax post,jQuery ajax调用web服务(asmx)触发认证弹出框

    在我的asp.net 4.0电子商务web应用程序中,登录的客户可以点击锚点,如"过去6个月内的订单"或"订单去年"来查看他过去的订单.当点击这样的锚,我做一个 ...

  5. jquery ajax node,jquery ajax post json and node express

    在开发中,我们需要通过ajax向node服务端发送数据,而JavaScript天然对json的解析极其友好,所以我们一般都是通过ajax向服务端发送一个对象,直接在服务端使用点操作获取对象内容. fr ...

  6. php jquery ajax登录,jQuery+Ajax+PHP弹出层异步登录效果(附源码下载)

    弹出层主要用于展示丰富的页面信息,还有一个更好的应用是弹出表单层丰富交互应用.常见的应用有弹出登录表单层,用户提交登录信息,后台验证登录成功后,弹出层消失,主页面局部刷新用户信息.本文我们将给大家介绍 ...

  7. 文本框输入即时Ajax搜索,JQuery+AJAX实现搜索文本框的输入提示功能

    平时使用谷歌搜索的时候发现只要在文本框里输入部分单词或字母,下面马上会弹出一个相关信息的内容框可供选择.感觉这个功能有较好的用户体验,所以也想在自己的网站上加上这种输入提示框. 实现的原理其实很简单, ...

  8. php jquery ajax xml,jquery Ajax解析XML数据代码实例

    jquery Ajax解析XML数据(同步及异步调用)简单实例 $.ajax({ async: true, // 默认true(异步请求) cache: true, // 默认true,设置为 fal ...

  9. jq ajax xml,jQuery+ajax读取并解析XML文件的方法

    本文实例讲述了jQuery+ajax读取并解析XML文件的方法.分享给大家供大家参考,具体如下: ajax.xml: zhangsan 1 lisi 2 demo.html: /p> " ...

  10. json ajax查询,jQuery AJAX和JSON性能查询

    我将一些JSON数据存储在文本文件中,以便在我的页面中使用jQuery Ajax进行查询.目前,我的文本文件包含大约10个数据面(可能包含额外的30个数据面). JSON数据包含这些问题的问题和答案. ...

最新文章

  1. 一文详解Inception家族的前世今生(从InceptionV1-V4、Xception)附全部代码实现
  2. 寒霜朋克计算机丢失,寒霜朋克玩不了怎么办 寒霜朋克进不去解决方法
  3. gan pytorch 实例_GaN教程(1)|美国宜普(EPC)公司推出“如何使用氮化镓器件”系列视频教程,讲授GaN功率晶体管和集成电路设计的方方面面...
  4. python判断英文字母_python判断字符串中是否含有英文 | 个人学习笔记记录
  5. phpcmsV9 如何开启“会员注册” - 教程篇
  6. Lua 中的 function、closure、upvalue
  7. GDAL读写矢量文件——Java
  8. foxmail本地文件夹同步服务器,foxmail同步QQ邮箱里的所有文件夹
  9. 透过表象看本质!?之三——Kalman滤波
  10. 扩展欧几里德算法解决问题A:Jugs
  11. 【BZOJ 2054】 疯狂的馒头
  12. WDA-FPM-4-用OVP做查询跳转到明细
  13. 【Vue】全局变量的定义及使用
  14. php落寞了,日渐唱衰的php是否真的已经不堪一击了?
  15. 对于十进制转化为任意进制数的简单算法分析
  16. wps VBA 更改模块的名字,VBA模块重命名,坑
  17. 2021年「博客之星」参赛博主:dnbug Blog
  18. 如何联系NOI各省特派员?
  19. 从公司管理到IT审计-网摘
  20. 全网最强在线解析源码

热门文章

  1. 郎咸平:诸葛亮是一名优秀的企业家吗?
  2. 造成java.io.IOException: Stream Closed异常的代码
  3. springboot打包成jar包后找不到xml,找不到主类的解决方法
  4. 怎样解决样本不平衡问题
  5. 基于FastJson的通用泛型解决方案
  6. 如何使用jQuery获取焦点元素?
  7. 为所有服务器端代码调用ConfigureAwait的最佳实践
  8. 什么是未定义的引用/未解决的外部符号错误,如何解决?
  9. 有没有一种简单的方法可以按值删除列表元素?
  10. 将JavaScript字符串全部转换为小写吗?