English 中文(简体)
在回应后如何获取js值?
原标题:How can i access a js value in post response?

我正请求从服务器获取 html 代码 的一部分 。 服务器上有一个文件, 要为不同的要求打印 html 代码 。 我在此使用以下代码 :

function check(inf_type) {
    $.ajax({
        type:  POST ,
        url: "get_info.php",
        data: { "sequence_no" : 1 },
        success: function(data) {
            // how can i use value of variable "inf_type" here.
            // here, the variable "data" contains HTML code.
        },
        dataType:  text 
    });
}

the function check() accepts a parameter inf_type which contains random strings according to which, server recognize the html code to print. Now, i want handle the POST response according to this inf_type. How can i access the value of inf_type variable in POST response function? The function check() is called more often, thats why i can not put the inf_type variable value in any global variable. What can i do to achieve that? Please guide me. thanks in advance.

最佳回答

您可以通过 inf_type 参数访问 check () 函数 :

function check(inf_type) {
    $.ajax({
        type:  POST ,
        url: "get_info.php",
        data: { "sequence_no" : 1 时 时,
        success: function(data) {
            if (inf_type == 0) {
                // do something with data
            时 时 else {
                // do something else
            时 时
        时 时,
        dataType:  text 
   时 时);

时 时

之所以这样做,是因为内部函数(成功回调)可以访问外部函数中的变量( check) 。 详情请参见此答案 : < a href="https://stackoverflow.com/ a/1111200/69868" >https://stackoverflow.com/ a/11100/69868

Edit This assumes that inf_type is a either a number or a new (different) instance of object in each call of check(). Details are explained in the link mentioned above.

问题回答

您可以在成功函数中使用info_type变量。成功函数中仍然存在参数info_type的范围。

您可以在成功或错误函数中直接使用变量。

function check(inf_type) {
    $.ajax({
        type:  POST ,
        url: "get_info.php",
        data: { "sequence_no" : 1 },
        success: function(data) {
            alert(inf_type); //inf_type is available here.
        },
        dataType:  text 
    });
}

首先,您必须向服务器发送 < 坚固> inf_ type , 以使特定值返回像这样的返回 :

function check(inf_type) {
 $.ajax({
    type:  POST ,
    url: "get_info.php",
    data: { "sequence_no" : 1, whatToSearch : inf_type }, // i added
    success: function(data) {
         //$(selector).html(data);//where you want to show the data
        // how can i use value of variable "inf_type" here.
        // here, the variable "data" contains HTML code.
    },
    dataType:  text 
 });
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签