From e337fb35fc11be42f449534e45bb625ff0097281 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 2 Jan 2014 21:59:25 +0800 Subject: [PATCH] closed #3291: XMLHttpRequest.status needs to be assigned even when connection fails. --- .../bindings/network/XMLHTTPRequest.cpp | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/cocos/scripting/javascript/bindings/network/XMLHTTPRequest.cpp b/cocos/scripting/javascript/bindings/network/XMLHTTPRequest.cpp index 8201955869..6581d93205 100644 --- a/cocos/scripting/javascript/bindings/network/XMLHTTPRequest.cpp +++ b/cocos/scripting/javascript/bindings/network/XMLHTTPRequest.cpp @@ -174,15 +174,13 @@ void MinXmlHttpRequest::handle_requestResponse(cocos2d::network::HttpClient *sen CCLOG("%s completed", response->getHttpRequest()->getTag()); } - int statusCode = response->getResponseCode(); - char statusString[64] = {}; - sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag()); + long statusCode = response->getResponseCode(); + char statusString[64] = {0}; + sprintf(statusString, "HTTP Status Code: %ld, tag = %s", statusCode, response->getHttpRequest()->getTag()); if (!response->isSucceed()) { - CCLOG("response failed"); - CCLOG("error buffer: %s", response->getErrorBuffer()); - return; + CCLOG("Response failed, error buffer: %s", response->getErrorBuffer()); } // set header @@ -207,7 +205,7 @@ void MinXmlHttpRequest::handle_requestResponse(cocos2d::network::HttpClient *sen _status = 200; _readyState = DONE; - _dataSize = buffer->size(); + _dataSize = static_cast(buffer->size()); CC_SAFE_FREE(_data); _data = (char*) malloc(_dataSize + 1); _data[_dataSize] = '\0'; @@ -546,17 +544,23 @@ JS_BINDED_PROP_SET_IMPL(MinXmlHttpRequest, withCredentials) */ JS_BINDED_PROP_GET_IMPL(MinXmlHttpRequest, responseText) { - jsval strVal = std_string_to_jsval(cx, _data); - - if (strVal != JSVAL_NULL) + if (_data) { - vp.set(strVal); - //JS_ReportError(cx, "Result: %s", data.str().c_str()); - return JS_TRUE; - } else { - JS_ReportError(cx, "Error trying to create JSString from data"); - return JS_FALSE; + jsval strVal = std_string_to_jsval(cx, _data); + + if (strVal != JSVAL_NULL) + { + vp.set(strVal); + return JS_TRUE; + } } + + CCLOGERROR("ResponseText was empty, probably there is a network error!"); + + // Return an empty string + vp.set(std_string_to_jsval(cx, "")); + + return JS_TRUE; } /**