javascript 对象不支持属性或方法“替换”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7647490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:48:04  来源:igfitidea点击:

Object doesn't Support Property or Method 'replace'

javascriptjquery

提问by V.B

I am getting a strange error in my javascript code.

我的 javascript 代码中出现一个奇怪的错误。

Here is the code sample

这是代码示例

function FetchData()
{
var selValue=$("select[id$=ddlComponents]").val()
    var param=$.param({ID:selValue});

    var method="proxy.aspx/GetComponentsValuesAgainstOilValue";

$.ajax({
    type: "POST",
    url: method,
    data: param,
    contentType: "application/json",
    dataType: "json",
    success: function(response) {

    if (response.replace(/"/g, '') == '{d:[]}') 
    {
         response = eval('(' + response + ')').d;
     }

    },
    error: function(xhr,error,status)
    {   
        alert(error);
    }
  });

}

}

It gives me an error at following line of code

它在以下代码行中给了我一个错误

if (response.replace(/"/g, '') == '{d:[]}') 
    {
         response = eval('(' + response + ')').d;
    }

object does not support property or function 'replace'. But replace function is working with string variables otherwise.

对象不支持属性或函数“替换”。但是替换函数正在处理字符串变量。

My JQuery ver is 1.6.4

我的 JQuery 版本是 1.6.4

Please help.

请帮忙。

Thanks vivek

谢谢维维克

回答by ThiefMaster

responseis already an object. You don't need to do any JSON parsing on your own.

response已经是一个对象。您不需要自己进行任何 JSON 解析。

回答by Bas Slagter

The type of response is clearly not an object of the type string. Try to parse it as a string or look what's inside the object that is currently being returned and use that in a proper way. In other words...see what your method "GetComponentsValuesAgainstOilValue" is returning to the client. That's probably what is in your response object at the moment (in JSON).

响应的类型显然不是字符串类型的对象。尝试将其解析为字符串或查看当前返回的对象内部的内容并以适当的方式使用它。换句话说......看看你的方法“GetComponentsValuesAgainstOilValue”正在返回给客户端。这可能是您目前的响应对象中的内容(在 JSON 中)。

回答by Przemek

The data parameter taken by success callback is formatted according to the dataType parameter. In your case - "json", so your data is an object.

成功回调获取的数据参数根据 dataType 参数进行格式化。在你的情况下 - “json”,所以你的数据是一个对象。

回答by Sarath Avanavu

Some versions of JQuery doesn't support the method. U need to get the following versions of JQuery

某些版本的 JQuery 不支持该方法。你需要获得以下版本的 JQuery

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>