如果从JSON.search()返回JavaScript变量,为什么它是“未定义的”?

时间:2022-10-24 22:39:15

I have the following code

我有以下代码

function f () {
  var jsonvar = get (...);
  console.log (jsonvar);
  $.ajax({
      type: "POST",
      url: transactUrl,
      dataType: "xml",
      success: function (result) {
        console.log (jsonvar);
      }
  });
}

function get (param) {
    return JSON.search (...).toString ();
}

and I will have result:

我会得到结果:

"someValue"
undefined

But why is jsonvar "forgotten" inside of $.ajax() function? Variable which is not defined with JSON will have the same value. What is the difference between them? Javascript's typeof returns String for both of them.

但为什么jsonvar“忘记”在$ .ajax()函数内?未使用JSON定义的变量将具有相同的值。它们之间有什么区别? Javascript的typeof为它们返回String。

2 个解决方案

#1


2  

I think you have some error somewhere in your code that is (possibly) causing a "silent error". See this fiddle; http://jsfiddle.net/9k1f6dpb/

我认为你的代码中某处有一些错误(可能)导致“无声错误”。看到这个小提琴; http://jsfiddle.net/9k1f6dpb/

function f () {
    var jsonvar = get();
    console.log (jsonvar);

    $.ajax({
        type: "GET",
        url: 'http://updates.html5rocks.com',
        success: function (result) {
            console.log(jsonvar);
        }
    });
}

function get (param) {
    return JSON.search({'a': 1}, '//a').toString();
}

f();

This shows that the setup is working and the variable is not "forgotten" along the way. Try adding 'use strict'...it might help throwing useful errors while debugging.

这表明设置正在运行,并且变量在此过程中不会被“遗忘”。尝试添加'use strict'...它可能有助于在调试时抛出有用的错误。

function f () {
    'use strict';
    ...

#2


0  

I found out the problem:

我发现了问题:

ii = "someString";
function f () {
    console.log(ii);
    var ii = 5;
    console.log(ii);
}
f();

gives output:

undefined
5

So the same variable was defined inside of the function. But this is new to me, I thought that variable will exist when it is first time defined.

因此在函数内部定义了相同的变量。但这对我来说是新的,我认为变量将在第一次定义时存在。

#1


2  

I think you have some error somewhere in your code that is (possibly) causing a "silent error". See this fiddle; http://jsfiddle.net/9k1f6dpb/

我认为你的代码中某处有一些错误(可能)导致“无声错误”。看到这个小提琴; http://jsfiddle.net/9k1f6dpb/

function f () {
    var jsonvar = get();
    console.log (jsonvar);

    $.ajax({
        type: "GET",
        url: 'http://updates.html5rocks.com',
        success: function (result) {
            console.log(jsonvar);
        }
    });
}

function get (param) {
    return JSON.search({'a': 1}, '//a').toString();
}

f();

This shows that the setup is working and the variable is not "forgotten" along the way. Try adding 'use strict'...it might help throwing useful errors while debugging.

这表明设置正在运行,并且变量在此过程中不会被“遗忘”。尝试添加'use strict'...它可能有助于在调试时抛出有用的错误。

function f () {
    'use strict';
    ...

#2


0  

I found out the problem:

我发现了问题:

ii = "someString";
function f () {
    console.log(ii);
    var ii = 5;
    console.log(ii);
}
f();

gives output:

undefined
5

So the same variable was defined inside of the function. But this is new to me, I thought that variable will exist when it is first time defined.

因此在函数内部定义了相同的变量。但这对我来说是新的,我认为变量将在第一次定义时存在。