如何获得AJAX后的返回值? [英] How to get AJAX post return value?

查看:95
本文介绍了如何获得AJAX后的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AJAX函数可以在.ashx文件中发布数据处理。

  var  b = ; 
$ .ajax({
async: true
contentType:' application / json; charset = utf-8'
type: POST
dataType:' json'
data: JSON .stringify(arrays),
url: MyHandler.ashx
成功:功能(结果){
b = true ;
},
错误: function (){
alert(' 发生错误');
}
});



我想在成功运行后设置b的值。我在b = true时设置了一个断点;如果它成功运行。但是程序在断点处没有停止运行。这意味着我实际上没有得到回归。我的代码有什么问题?谢谢。



我的尝试:



如何获取 AJAX Post  return   value 

解决方案

.ajax({
async: true
contentType:' application / json; charset = utf-8'
类型: POST
dataType:' json'
data: JSON .stringify(数组),
url: MyHandler.ashx
成功:< span class =code-keyword> function (result){
b = true ;
},
error : function (){
alert(' 发生错误' );
}
});



我想在成功运行后设置b的值。我在b = true时设置了一个断点;如果它成功运行。但是程序在断点处没有停止运行。这意味着我实际上没有得到回归。我的代码有什么问题?谢谢。



我的尝试:



如何获取 AJAX Post  return   value 


使用b的任何代码也需要进入成功事件,或者将其放入成功事件的函数中代码电话。



<前lang =JavaScript>成功:功能(结果){
b = true ;
流程(b);
},





  function  process(result){
if (result){
//
}
}





您的ajax电话是异步的,因此您的代码不会在


.ajax行停止,它会在后台完成ajax调用后继续执行代码。更便宜的替代方法是保持代码不变,并通过添加



async:false

<使ajax调用同步而不是异步br />
选项。



jQuery。 ajax()| jQuery API文档 [ ^ ]


I have a AJAX function to post for data processing in an .ashx file.

var b = false;
$.ajax({
    async: true,
    contentType: 'application/json; charset=utf-8',
    type: "POST",
    dataType: 'json',
    data: JSON.stringify(arrays),
    url: "MyHandler.ashx",
    success: function (result) {
        b = true;
    },
    error: function () {
        alert('Error occurred');
    }
});


I want set the value of b after it runs successfully. I set a break point at b = true; if it runs successfully. But the program runs without stopping at the break point. It means I did not get the Post Return actually. What's wrong in my code? Thanks.

What I have tried:

How to get AJAX Post return value?

解决方案

.ajax({ async: true, contentType: 'application/json; charset=utf-8', type: "POST", dataType: 'json', data: JSON.stringify(arrays), url: "MyHandler.ashx", success: function (result) { b = true; }, error: function () { alert('Error occurred'); } });


I want set the value of b after it runs successfully. I set a break point at b = true; if it runs successfully. But the program runs without stopping at the break point. It means I did not get the Post Return actually. What's wrong in my code? Thanks.

What I have tried:

How to get AJAX Post return value?


Whatever code you have that uses "b" needs to go in the success event also, or put it in a function that the success event code calls.

success: function (result) {
        b = true;
        process(b);
    },



function process(result){
if (result){
    //
}
}



Your ajax call is asynchronous so your code doesn't halt at the


.ajax line, it continues on to the code after while the ajax call completed in the background. The cheaper alternative is to leave your code as it is and make the ajax call synchronous rather than asynchronous by adding

async: false

to the options.

jQuery.ajax() | jQuery API Documentation[^]


这篇关于如何获得AJAX后的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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