Merge pull request #4807 from dumganhar/iss3291-xmlhttprequest-error-fix-develop

closed #3291: XMLHttpRequest.status needs to be assigned even when connection fails.
This commit is contained in:
James Chen 2014-01-02 06:46:25 -08:00
commit 831e0b08e0
1 changed files with 20 additions and 16 deletions

View File

@ -174,15 +174,13 @@ void MinXmlHttpRequest::handle_requestResponse(cocos2d::network::HttpClient *sen
CCLOG("%s completed", response->getHttpRequest()->getTag()); CCLOG("%s completed", response->getHttpRequest()->getTag());
} }
int statusCode = response->getResponseCode(); long statusCode = response->getResponseCode();
char statusString[64] = {}; char statusString[64] = {0};
sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag()); sprintf(statusString, "HTTP Status Code: %ld, tag = %s", statusCode, response->getHttpRequest()->getTag());
if (!response->isSucceed()) if (!response->isSucceed())
{ {
CCLOG("response failed"); CCLOG("Response failed, error buffer: %s", response->getErrorBuffer());
CCLOG("error buffer: %s", response->getErrorBuffer());
return;
} }
// set header // set header
@ -207,7 +205,7 @@ void MinXmlHttpRequest::handle_requestResponse(cocos2d::network::HttpClient *sen
_status = 200; _status = 200;
_readyState = DONE; _readyState = DONE;
_dataSize = buffer->size(); _dataSize = static_cast<uint32_t>(buffer->size());
CC_SAFE_FREE(_data); CC_SAFE_FREE(_data);
_data = (char*) malloc(_dataSize + 1); _data = (char*) malloc(_dataSize + 1);
_data[_dataSize] = '\0'; _data[_dataSize] = '\0';
@ -545,20 +543,26 @@ JS_BINDED_PROP_SET_IMPL(MinXmlHttpRequest, withCredentials)
* *
*/ */
JS_BINDED_PROP_GET_IMPL(MinXmlHttpRequest, responseText) JS_BINDED_PROP_GET_IMPL(MinXmlHttpRequest, responseText)
{
if (_data)
{ {
jsval strVal = std_string_to_jsval(cx, _data); jsval strVal = std_string_to_jsval(cx, _data);
if (strVal != JSVAL_NULL) if (strVal != JSVAL_NULL)
{ {
vp.set(strVal); vp.set(strVal);
//JS_ReportError(cx, "Result: %s", data.str().c_str());
return JS_TRUE; return JS_TRUE;
} else {
JS_ReportError(cx, "Error trying to create JSString from data");
return JS_FALSE;
} }
} }
CCLOGERROR("ResponseText was empty, probably there is a network error!");
// Return an empty string
vp.set(std_string_to_jsval(cx, ""));
return JS_TRUE;
}
/** /**
* @brief get response of latest XHR * @brief get response of latest XHR
* *