jQuery的Ajax调用 - 设置成功变量的值 [英] jQuery Ajax call - Set variable value on success

查看:122
本文介绍了jQuery的Ajax调用 - 设置成功变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我写的修改,在服务器缓存对象数据的应用程序。这些修改是通过Ajax调用,基本上更新该对象的属性进行。当用户完成工作,我有一个基本的保存更改按钮,允许用户保存数据并刷新缓存的对象。

I have an application that I am writing that modifies data on a cached object in the server. The modifications are performed through an ajax call that basically updates properties of that object. When the user is done working, I have a basic 'Save Changes' button that allows them to Save the data and flush the cached object.

为了保护用户,我要警告他们,如果尝试导航离开该页面时已经过修改,给服务对象,如果他们已经没救了。所以,我创建了一个名为将IsInitialized Web服务方法,将根据是否不更改已保存返回true或false。如果它们没有被保​​存,我想提示用户,并给他们一个机会,取消其导航请求。

In order to protect the user, I want to warn them if the try to navigate away from the page when modifications have been made to the server object if they have not saved. So, I created a web service method called IsInitialized that will return true or false based on whether or not changes have been saved. If they have not been saved, I want to prompt the user and give them a chance to cancel their navigation request.

下面是我的问题 - 虽然我有正常工作的电话,我似乎无法获得成功的AJAX调用设置它的回调函数变量的值。这里的code我现在有。

Here's my problem - although I have the calls working correctly, I can't seem to get the ajax success call to set the variable value on its callback function. Here's the code I have now.

   ////Catches the users to keep them from navigation off the page w/o saved changes...
window.onbeforeunload = CheckSaveStatus;
var IsInitialized;

function CheckSaveStatus() {

    var temp = $.ajax({
    	type: "POST",
    	url: "URL.asmx/CheckIfInstanceIsInitilized",
    	data: "{}",
    	contentType: "application/json; charset=utf-8",
    	dataType: "json",
    	success: function(result) {
    		IsInitialized = result.d;
    	},
    	error: function(xmlHttpRequest, status, err) {
    		alert(xmlHttpRequest.statusText + " " + xmlHttpRequest.status + " : " + xmlHttpRequest.responseText);
    	}

    });

    if (IsInitialized) {
    	return "You currently have unprocessed changes for this Simulation.";
    }
}

我觉得我可能会尝试使用成功回调以不适当的方式。如何设置上的成功回调的JavaScript变量,这样我就可以决定用户是否应该提示瓦特/未保存的更改消息?

I feel that I might be trying to use the Success callback in an inappropriate manner. How do I set a javascript variable on the Success callback so that I can decide whether or not the user should be prompted w/ the unsaved changes message?

正如刚才所说,我想提出一个异步调用,这意味着在code,其余我的方法返回之前被调用。有没有一种方法来使用Ajax调用,但还是赶上window.onunload事件? (未做synchronos AJAX)

As was just pointed out, I am making an asynchronous call, which means the rest of the code gets called before my method returns. Is there a way to use that ajax call, but still catch the window.onunload event? (without making synchronos ajax)

推荐答案

由于需要在卸载时这种行为,你将不得不作出一个同步调用。然而,它可能会冻结浏览器窗口/标签依赖于呼叫需要多长时间,但因为你有效地试图prevent在关闭窗口的用户...

Since you need this behavior in the unload event, you will have to make a synchronous call instead. However it may freeze the browser window/tab dependent on how long the call will take, but because you're effectively trying to prevent the user from closing the window...

添加异步:假您的JSON进行同步调用

Add async: false to your JSON to make a synchronous call.

这篇关于jQuery的Ajax调用 - 设置成功变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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