amitshree
amitshree

Reputation: 2298

Ajax response status 200 but shows error message

I'm getting status as 200 but it's printing message present inside error message alert("error...");. Why so?

function makeSelect() {
    var blouseoption = document.querySelector('input[name="blouseoption"]:checked').value;
    var url = "http://dukano.co/sakhidev/retailon/productoption/values";
    alert(url);
    var jsondata = $j('#customoption').serialize();     
    alert("jsondata: " + JSON.stringify(jsondata));

    $j.ajax({    
        type : 'POST',
        url : url,
        data : jsondata,
        dataType : 'json',
        success : function(response) {
            console.log("calling");
            console.log(response);
            alert("call success");
            alert("response data:" + JSON.stringify(response));  
            if (response.status == 200) {
                console.log("yes");
            } else if (response.status == "error") {
                console.log("no");
            }

        },
        error : function(response) {
            alert("error...");
            alert("response:" + JSON.stringify(response));
            console.log(response);
        }
    });
}

Magento's controller function returning json value

  public function valuesAction(){
            $blouseoption = $this->getRequest()->getParam('blouseoption');
            $sareefinishing = $this->getRequest()->getParam('sareefinishing');
            $data = array( 'sfinishing' => $sareefinishing, 'layout' => $this->getLayout());
            Mage::dispatchEvent('product_custom_option', $data);
            $jsonData = json_encode(array($blouseoption, $sareefinishing));
            $this->getResponse()->clearHeaders()
             ->setHeader('Content-type','application/json',true);
            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($jsonData));
            $this->getResponse()->sendResponse();
    }

Upvotes: 1

Views: 9112

Answers (2)

Nithin Baby
Nithin Baby

Reputation: 7516

Alternative answer

Instead of returning status 200 with empty response, you can return status 204 and not return any response. Status 204 is for No Content. JQuery should not throw any error in this case.

Upvotes: 0

tech-gayan
tech-gayan

Reputation: 1413

As you are using

 dataType: "json"

this evaluates the response as JSON and returns a JavaScript object.any malformed JSON is rejected and a parse error is thrown.

This means that if server returns invalid JSON with a 200 OK status then jQuery fires the error function and set the textStatus parameter to "parsererror".

Make sure that the server returns valid JSON. empty response is also considered invalid JSON; you could return {} or null for example which validate as JSON. try to check the textStatus in the error.

 error : function(jqXHR,textStatus,errorThrown)
   {console.log(textStatus)}

if this prints "parsererror" then of course you have problem with your returning json. please check that.

More Info

Upvotes: 1

Related Questions