How to receive data from an Iframe in Dart? - iframe

I know that the only way to communicate with an iframe is using the postMessage method as follow:
iframe.contentWindow.postMessage("my message", window.location.href);
but I would like to know how to receive data from the iframe after any change on it.

In the receiver window, when you receive the message, you get the sender passed with the message and you can send back to this sender.
window.onMessage.listen((e) {
e.source.postMessage({'somedata': 'xxx'}, '*');
});
See also
https://github.com/dart-lang/sdk/issues/26351
https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel

Related

Angular Push notification error in service worker, Unexpected token P in JSON at position 0

I am trying to use push notification in a pwa, but it is failing to receive the notification.
On the client side I am receiving the following error
Uncaught SyntaxError: Unexpected token P in JSON at position 0
at Driver.onPush (ngsw-worker.js:1967)
at ngsw-worker.js:1871
I add an image that includes the data that reaches the service worker
As you can see in the previous image, the data field is empty, however when I send it I am adding a text
To test the application I am releasing the notification with the following:
https://web-push-codelab.glitch.me/
I already appreciate any contribution.
If your payload is in any json format, like the one above, the error does not appear, but the notification is not shown in your client and no other error message is shown. You have to use a certain json format. One minimal working payload is:
{ "notification": {"title": "message title", "body": "message body"} }
I haven't found the exact and complete spec for this json, but here (search for "payload") you'll find a more complete example.
Make sure you are correctly sending the payload.
Everything is fine except ngsw-worker can't parse the payload.
If you are on windows, try this:
--payload="{\"hello\":\"world\"}"
And not this:
--payload='{"hello":"world"}'
Example (using webpush cli):
web-push send-notification --endpoint="https://fcm.googleapis.com/fcm/send/xxx:xxx" --key="xxxxxxxxxxxxx" --auth="xxxxxxxxxx" --payload="{\"hello\":\"world\"}" --vapid-subject="https://localhost:8000" --vapid-pubkey=xxxxxxxxxx --vapid-pvtkey=xxxxxxxx

$http returning error response NULL on first call after launch (ionic) everytime, but after subsequent http post its ok

Whenever I launch my app, and click on login on the first few tries, the login will attempt a POST http to the server. However $http always (everytime) returns NULL on first try. sometimes after several few tries still NULL if done fast. But subsequently, its all ok.
I dont get it, why is $http returning error response NULL initially ??
Here is my login controller doing the http post
Login Controller (LoginCtrl)
https://gist.github.com/anonymous/771194bc5815e4ccdf38b57d6158853f
var req = {
method: 'POST',
url: baseURL,
data: postObject,
//timeout: 5000
};
err is NULL here:
}).error(function(err) {
I dont know if it is CORS but I'ved got this set in config.xml
<access origin="*" />
my config.xml
https://gist.github.com/anonymous/b2df3a857338d14ec3fcd6dda776e212
Any ideas ?
Im using ionic 1.7.14
on device iOS 9.3.1
UPDATE
I'ved put the problem code here. can logout first to goto login screen. enter in anything in username/password field, click login once failed, second or third try will be success.
https://github.com/axilaris/ionic_null_http_problem
some troubleshooting so far: i noticed the http post request is called twice. not sure why.
UPDATED the code using $http.post.then but still has the same effect
$http.post(baseURL, postObject).then(function successCallback(response)
response has NULL data --> Object {data: null, status: 0, config: Object, statusText: ""}
It is hard to diagnose having the above details only.
However the problem could be that your handler (login function) is triggered before digest cycle finished updating $scope.data.username and $scope.data.password and for the first tries it sends empty values for those to the server and works fine later.
You can run Safari web inspector to see what is sent to the server to prove this.
The fix may depend on how your view/template is coded. Can you please share it? Or, ideally, create a working sample at http://play.ionic.io/
Another option to fix could be to try to wrap your code related to http request into
$timeout(function() {
// your code goes here
});
or, consider using .$applyAsync() (see the docs for details)
This might help to fix the problem
You are probably getting this inconsistent behavior as you are using the 'success' promise method instead of 'then' (note that use of the success method has now been deprecated).
The key differences between these two methods are:
then() - full power of the promise API but slightly more verbose
success() - doesn't return a promise but offeres slightly more convienient syntax
as highlighted in this answer.
Hence in your scenario, instead of using 'success':
var req = {
method: 'POST',
url: baseURL + 'session/login',
data: postObject,
//timeout: 5000
};
$http(req).success(function(resp) {...
use 'then' along with angular's post shortcut method (you don't have to use this shortcut method, but I think it makes the code more succinct) e.g.:
$http.post(baseURL + 'session/login', postObject).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
Using 'then' returns a promise resolved with a value returned from a callback, so it should give you a consistently valid result.
it was a timeout in app.js that caused it. was set to 1 second which gives it it arbitrary success rate.
config.timeout = 1000;

Using caller id as twilio number in asp.net mvc

I've opened up a twilio trial account. I want to use my verified called ID number in my asp.net application instead of buying a new twilio number. But I don't know how to use it. Here are my codes,
public ActionResult SendSms()
{
string ACC_SID = ConfigurationManager.AppSettings["ACC_SID"];
string AUTH_TKN = ConfigurationManager.AppSettings["AUTH_TKN"];
TwilioRestClient client = new TwilioRestClient(ACC_SID, AUTH_TKN);
client.SendMessage("+8801941234567", "+8801671234567", string.Format("This is a test message!"));
return RedirectToAction("SmsView");
}
Nothing happened with it. How can I use my called ID to send sms?
I faced the same scenario while I was working on Twilio first time and at that time I used below steps to make things workable.
1) First of all I would suggest please add Try{} catch{} block in your SendSMS function. this will give you idea like SendMessage worked correctly.
2) In twilio we need to register mobile no before send SMS.
3) if your function works fine(no error occur) then in Twilio there is LOG tab where you can see weather your SMS send correctly or not.
Twilio developer evangelist here.
Jinesh's answer has some useful parts, like using a try/catch block to see errors.
However, the problem is that you are trying to send an SMS from your own number, not from a Twilio number. When sending SMS, you can only send from Twilio numbers. Please see this page for details on why.
You should have got a Twilio number when you signed up. Try using that number as the From number instead.

How to get received sms from sms web service without page refresh and auto

I have a web service for send sms from website. I can get the received sms by calling Page_load codes. But I want get this messages automatically then send a message(sms) to sender. It means I want relpay to his sms without page load or refresh page automatically.
Put your data into a div, Use ajax and call your ajaxified function after every desired time using interval. ...
some thing like
$(document).ready(function(){
var refresh = setInterval(function(){
// Do something every 1 seconds like call an ajax function
$('#youdiv').ajax(); //this is dummy, for right syntax follow the link at the end of answer
}, 1000); // 1000 equals to 1 sec
});
Read more about ajax
http://api.jquery.com/jQuery.ajax/

Cancel form.submit()

In javascript function I am submitin a form:
form.submit();,
with huge amount of data.
Is it possible to cancel this operation beside clicking on browser's stop button?
UPDATE:
Maybe i didn't made my self clear i am submiting large files.
form with uploadcontrol that runs in iframe.
You could do something like what Gmail does: Undo Sending Mails (lab feature).
It justs delays the mail sending by a 5 seconds. If in that time the server receives a undo AJAX call, the mail is not send.
This could potentially increase you serverside complexity, but not that much.
In the case that the information is not sent you could use this:
//IE
document.execCommand('Stop');
//Firefox
window.stop();
So the whole thing would look something like this (pseudocode):
stop_submit(current_id){
try{
document.execCommand('Stop');
}catch(e){}
try{
window.stop();
}catch(e){}
AJAXCall("/myfolder/cancel_submit/", "id="+current_id, "post");
return;
}
This is a classic problem with the client-server architecture. Once the HTTP request has been made, there is no stopping it. You will need a way to restore state after all requests to guarantee that everything is back to the way it was.
Now, you could do this with AJAX and use the abort function. Or, you could try window.stop() or document.execCommand("Stop"), depending on your browser. But those do not solve the problem of the request having already been sent.

Resources