|
<< Click to Display Table of Contents >> Navigation: Forum > post xml data with XMLHttpRequest |
08-23-2024, 03:26 AM:
Hi ATOZED,
Similar to the example "PostDataDemo", I want to query XML data via a corresponding content handler, but my content handler cannot find the query data.
C++ Builder 12 Patch 1
Edition: IntraWeb Ultimate Edition
IntraWeb Version: 15.6.3
32-bit Server on 64-bit Windows
HTTP Server is active on port: 80
HTTPS Server is active on port: 543 (OpenSSL v1.1.1)
In the server controller, the content handler is configured as follows:
this->SecurityOptions->HttpMethods << hmGet << hmPost;
this->SecurityOptions->ForceAjaxPost = true;
TIWMimeTypes::RegisterType(L".xml", L"application/xml; charset=UTF-8", false);
THandlers::Add(L"", L"Query.xml", new TIWH_Xml());
The query is made interactively via an XMLHttpRequest object (see appendix).
The HTML document is inserted into a frame of the MainForm.
The content handler is called correctly but the query data does not arrive:
aRequest->HasContent = true
aRequest->Files->Count = 0 !!!
The test tool specified in "How to simulate a request using Firefox Poster addon.png" (2017)
I could unfortunately no longer find it at Mozilla.
Is there something wrong with my example ?
Does anything else need to be configured ?
Regards
Juergen
08-23-2024, 10:36 PM:
Hey Juergen
I got nearly the same problem, and I could solve it by reading this thread:
https://www.atozed.com/forums/thread-1402.html
I add these in ServerController.cpp and it works:
void __fastcall TIWServerController::IWServerControllerBaseConfig(TObject *Sender)
{
TpaypalForm::SetURL("/", "paypalForm");
//RegisterContentType(L"application/xml");
RegisterContentType(L"application/json"); // <---------------------
}
Maybe it helps
Regards
Jörg
08-24-2024, 09:18 AM:
Hi Jörg
thanks for the hint.
I added the following entry in the Server Controller and it works,
the XML content is transferred as a file to the server cache.
// Content-Type from IW.Parser.Files.hpp
RegisterContentType(L"application/xml");
However, I now have a new problem ![]()
No matter how I try to read the file content in the content handler I always get an exception.
Message: "File ...\cache\01e73j1t0q\ATZFH~UucD5kIPr~szeJN7wnmOoy7W.tmp cannot be opened. The process cannot access the file because it is being used by another process."
Regards
Juergen
08-25-2024, 03:16 PM:
I used this code , it's the execute func of the content handler:
bool __fastcall TContentPP ::Execute(Iw::Http::Request::THttpRequest* aRequest, Iw::Http::Reply::THttpReply* aReply, const System::UnicodeString aPathname, Iwapplication::TIWApplication* aSession, System::Classes::TStrings* aParams)
{
bool rc = true;
UnicodeString empf;
THttpFile *xFile;
TFileStream *fs;
TStringStream *js;
if (aRequest->Files->Count > 0)
{
xFile = aRequest->Files->Items[0];
fs = new TFileStream(xFile->TempPathName, fmOpenRead | fmShareDenyNone);
js = new TStringStream;
js->CopyFrom(fs, 0);
empf = js->DataString;
delete fs;
delete js;
rc = true;
}
if (aReply)
{
aReply->ContentType = MIME_JSON;
aReply->Code = 200;
aReply->CodeText = "OK";
aReply->WriteString("{\"status\":\"success\"}"); // R?ckgabe eines JSON-Objekts
}
return rc;
}
//---------------------------------------------------------------------------
the string "empf" hold the json-reply I wait for.
08-26-2024, 09:19 AM:
I'am using the same code, there must be another reason.
08-26-2024, 02:57 PM:
hmmmm
I don't know the reason ... but maybe you can try a shorter filename . I.e. the component TIWURLWindow comes with a similar error , when trying such a long URI name. Don't know why, but a shorter filename works.
08-26-2024, 05:20 PM:
I'm not able to test this but what happens if you execute the code below to read the JSON?
Code:
...
if(aRequest->Files->Count > 0) {
String empf = aRequest->Files->Items[0]->ReadAllText();
}
...
08-26-2024, 05:41 PM:
I used the following code with the same result:
UnicodeString CurXmlStr = TIWTextFileReader::ReadAllText(aRequest->Files->Items[0]->TempPathName);
08-26-2024, 06:10 PM:
Did this 'aRequest->Files->Items[0]->ReadAllText();' work though? The idea being to use what's built in the THttpFile object and not open it a second time.
08-27-2024, 07:01 AM:
(08-26-2024, 06:10 PM)MJS@mjs.us Wrote: [ -> ]Did this 'aRequest->Files->Items[0]->ReadAllText();' work though? The idea being to use what's built in the THttpFile object and not open it a second time.
Worked, thanks a lot !