Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object doesn't Support Property or Method 'replace'

I am getting a strange error in my javascript code.

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

Please help.

Thanks vivek

like image 768
V.B Avatar asked Oct 04 '11 11:10

V.B


3 Answers

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

like image 171
ThiefMaster Avatar answered Nov 01 '22 17:11

ThiefMaster


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).

like image 21
Bas Slagter Avatar answered Nov 01 '22 17:11

Bas Slagter


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

like image 32
Przemek Avatar answered Nov 01 '22 17:11

Przemek