i have to send an array of objects like this
[{"Cod":"1"},{"Cod":"5"}]
to my C# WCF Service which i used until today without problems with wsHttpBinding (so no JSON using here, only XML).
The request, POST, contains this param:
let param = "<cod>" + data + "</cod>";
My problem is on WCF because i can't find a way to get that array of objects from the parameter of the method:
public string GetArrayOfObjects(CompositeType[] cod)
{//implementation not important because i can't enter here...}
where CompositeType is:
[DataContract]
public class CompositeType
{
string cod;
[DataMember]
public string Cod
{
get { return cod; }
set { cod = value; }
}
}
I have tried so many things like changing the parameter to:
Object[] cod
List<string> cod
...
Everytime i get this error (translating):
Exception generated by the formatter in an attempt to deserialize the message: Error trying to deserialize the parameter http://tempuri.org/:cod. InnerException: 'Error at line 1 position 341. Expected status 'Element' .. Found 'Text' with '' name space ''. '.
...
This is the post message that i'm sending:
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">" +
"<s:Header>" +
"<a:Action s:mustUnderstand=\"1\">http://tempuri.org/IService1/" + GetArrayOfObjects + "</a:Action>" +
"</s:Header>" +
"<s:Body>" +
"<" + GetArrayOfObjects + " xmlns=\"http://tempuri.org/\">" +
param +
"</" + GetArrayOfObjects + ">" +
"</s:Body>" +
"</s:Envelope>";
where param is defined before. The JSON.stringify of data inside param gives the first example of this question ([{"Cod":"1"},{"Cod":"5"}]). This post message was always fine with single value params.
Some sites show a solution with a json deserializer on wcf, but i'm using XML...it's not an option right now to re-implement the project in json.
I tried for one day but i didn't find a solution, i'd love to hear some solutions from you! Thanks to all.
Related
Im sending a stringified json that should have property:
recievedObject: {"code":"this is a code snippet"}
(the code property has no specified programming language)
and then parsing it like this:
var items = JSON.parse('<%- JSON.stringify(items) %>')
When I try to parse the stringified json I received I get parsing errors (im guessing its because of the code string often containing characters like " ', line breaks etc.)
Is there a way to bypass or get around this problem without changing the string in any way? (I was able to get this working after encrypting it, but I would prefer not to do this)
example of how the code string might look:
code: "router.get('/', function(req, res, next) {\n" +
' try {\n' +
' const cities = spreadsheet.getData()\n' +
'\n' +
' } catch(err) {\n' +
' console.log(err)\n' +
' }\n' +
'\n' +
" res.render('index', { cities: cities})\n" +
'})'
Hollo,
I need to create a form text that sends the variable in URLEncoded format.
I need this for send SMS with API with this parameters (GET):
Username
APIKEY
Number
Text (URLEncoded)
How can I create this?
Thanks a lot for the collaboration :)
Assuming you are only missing the string generation part (and not the whole html + javascript stuff), you may have a function like :
function generateRequest(username, apikey, number, text) {
var baseUrl = "http://your.base.url/sms";
return baseUrl +
"?Username=" + username +
"&APIKEY=" + apikey +
"&Number=" + number +
"&Text=" + encodeURIComponent(text);
}
for more details about the encodeURIComponent, read this => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
I hope it will help you.
I have recently experienced a problem when passing string parameters via JS to controller action if string contains #, it would ignore the next parameter.
JS
window.location.href = $ele.data("url") + "?title=" + $ele.data("title") + "&employeeId=" + $ele.data("employeeid") + "&suggestionId=" + $ele.data("suggestionid") + "&statusId=" + $ele.val() + "&communicationId=" + $ele.data("communicationid");
C#
public ActionResult StatusUpdate(string title, Guid employeeId, Guid suggestionId,
int statusId, Guid? communicationId)
{
var suggestion = new SuggestionView
{
SuggestionStatusId = statusId,
SuggestionId = suggestionId
};
return new HttpStatusCodeResult(200);
}
So if $ele.data("title") contains #, in the controller action the next parameter or more specifically employeeId in this particular case would come as NULL, and it wouldn't pass the data properly since employeeId is not a nullable Guid.
Why is a controller action ignoring parameters after the #?
I'm having trouble with string manipulation. I have some code written in java, that will use an xls translator to generate some html for me - in string form.
I use spring framework to communicate this string back to my web code, but when the string arrives in the javascript, it fails with an "invalid or unexpected token error. Furthermore, when the string is written to the console, it seems that the string now containts newline characters for each new tag.
For my javascript I really need the html to be all one line.
here are some code bits:
try {
SimpleResultSet rs = dbClient.executeQuery("select MediaContent from call where id = " + callID);
if (rs.next()) {
media = rs.getString("MediaContent");
mimeType = rs.getString("MediaTypeID");
if(media.startsWith("<?xml")) {
trace.info("XSLT: " + xltString);
trace.info("Database XML: " + media);
media = Transform(media, xltString, response);
//trace.info("result HTML: " + media);
if (!media.isEmpty()) {
media = media.replaceAll("\n\r", "")
.replaceAll("\n", "")
.replaceAll(System.lineSeparator(), "");
}
}
//media = media.replaceAll("\"","\\\\\"");
}
} catch (DBException e) {
trace.warning("Failed to get call content media for call id = " + callID, e);
return media;
}
trace.info("cleaned HTML: " + media);
return media == null ? "" : media;
}
At this point the trace printing out the cleaned HTML, shows the string all on one line, without any newline characters. The string is then propagated to the ModelAndView like this:
return new ModelAndView("media", "media", mediaStr);
and on the javascript side:
<script>
var contentString = "${media}";
document.getElementById("mediaContentIFrame").srcdoc = contentString;
it is the contentString variable on the javascript side that fails with the invalid or unexpected token error.
The contentString is used to initialise the srcdoc property of an IFrame.
You have already added two checks \r\n and \n.. Also add \r and i hope it will start working :)
I am trying to load a javascript in WebView to do some calculations and get the output in a string. I tried to use following code
string htmlFragment = "<html><head><script type='text/javascript'>" +
"function doubleIt(incoming){ " +
" var intIncoming = parseInt(incoming, 10);" +
" var doubled = intIncoming * 2;" +
" document.body.style.fontSize= doubled.toString() + 'px';" +
" return doubled.toString());" +
"};" +
"</script></head><body>" +
"<div id = 'myDiv'>I AM CONTENT</div></body></html>";
htmlView.NavigateToString(htmlFragment);
htmlView.LoadCompleted += async(s1,e1) =>
{
string result = await htmlView.InvokeScriptAsync("eval", new string[] { "doubleIt(25)" });
Debug.WriteLine(result);
};
Update
I am able to load simple javascript easily now based on help provided in the answer. But now I am facing issues when there is more than one function in javascript, I am getting an exception. I am trying the following code
string htmlFragment = #"<html><head><script type='text/javascript'>" +
"function a(){return 10;};" +
"function b(){return 20;};" +
"function c(){return 30;};" +
"return (a()*b()*c());" +
"</script></head><body>" +
"<div id = 'myDiv'>I AM CONTENT</div></body></html>";
Please suggest.
The documentation for this feature is really poor. It took me some time to figure out how to invoke Javascript in UWP WebView
When you first look at the function call webView.InvokeScriptAsync(string,string[]) your initial reaction is that they want the function name as the first parameter and then the function paramaeters as the string array. (mainly because the MSDN documentation says this)
Parameters
scriptName
Type: System.String [.NET] | Platform::String [C++]
The name of the script function to invoke.
arguments
Type: System.String[]
[.NET] | Platform::Array [C++]
A string array that
packages arguments to the script function.
HOWEVER, this is wrong and will lead to hours of head banging. REALLY, what they want is the word "eval" in the first parameter and then a string array of functions, and or commands you wish to eval
var value = await webViewer.InvokeScriptAsync("eval",
new string[]
{
"functionName(functionParams)"
});
Having worked with Microsoft APIs for a few years now I am convinced that this is not the intended way of consuming this function and is a bit of a hack. Unfortunately if you want to consume JavaScript this is the only way that I know that works currently.
Anthony,
Try to check your own suggestion:
await webViewer.InvokeScriptAsync("eval",
new string[]
{
"functionName(functionParams)"
});
or:
await webViewer.InvokeScriptAsync(functionName, new string[]{ functionParameters });
The same as Microsoft suggests, just you are limiting a function name by one ("eval") - not necessary. Trust me, you can use any function name, as I am now with UWP and before with windows phone hybrid apps.
The question is already 4 years old, but I'm coming to see why you were getting an empty string as a result.
In your example, the functions in JavaScript return integers while the expected value is of type string.
By modifying these functions and returning a string like this:
string htmlFragment = #"<html><head><script type='text/javascript'>" +
"function a(){return '10';};" +
"function b(){return '20';};" +
"function c(){return '30';};" +
"</script></head><body>" +
"<div id = 'myDiv'>I AM CONTENT</div></body></html>";
We get the good result on the way back.