I am attempting to send data back to my objective c program from javascript.
I found a very helpful question here that got me started, but I quickly ran into a problem: the post seems to be failing.
I also verified syntax at this helpful site.
I know this because I update the contents of a div on my page before and after the post call and I only see the update pre-post. I also have a breakpoint at the beginning of shouldStartLoadWithRequest and it's not firing.
Here's my simple javascript code:
function updateAction(obj) {
document.getElementById("status").innerHTML="sending:"+obj;
$.post("http://actionupdate", {"data":obj});
document.getElementById("status").innerHTML="sent";
}
So.. my question: what am I missing? What's wrong with my post?
From your syntax, I guess you are using jquery? The UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType: method does not seem to get called for ajax requests, which is what $.post() does. You have to use a normal post instead.
You may find UIWebViewDelegate not monitoring XMLHttpRequest? helpful.
Related
I faced few problems while using Chrome dev tool. Just want to know whether it's possible and if yes - how. Suggest I have a really massive client side, with hundred of responses per page.
How to find endpoint which handle the response? I mean the first place in js code where the response come in.
How to find the response by it content? For instance, I want to know in which response I've got 45902309509902 value from the table.
How to find endpoint which handle the response?
On the Network tab, you can see where the request was originated, it's the column labelled "Initiator:"
That has a link that will show you the code originating the ajax call (I assume by "response" you're talking about an ajax response). From there, you should be able to find the callback that request is associated with. A lot of times, if you use a library like jQuery, you'll be shown the jQuery code doing the request rather than yours. You can still find what you need, though, by using the un-minified version of the libray, setting a breakpoint on that code (perhaps even a conditional one on, say, the URL being requested), and then when the breakpoint is hit using the call stack to find out where in your code the call actually originates.
How to find the response by it content?
This will be slightly more difficult. Again in the Network tab, you can click each ajax request and see (and search through) the text of there response under the Response sub-tab.
Sorry if this question has been asked before, I've look for a while and haven't found anything related. I recently started delving into AJAX/Javascript and for fun I was curious if it was possible to log into Twitter via an AJAX call.
So I looked at the source code for the login page, and it looks like on line 198 and 202 (Chrome source code viewer) the username and password keys are set, respectively, as well as the page it's being sent to. So from there I wrote this:
$.post('https://twitter.com/sessions',
{"session[username_or_email]" : "USERNAME",
"session[password]": "PASSWORD"},
function(data) {
$('body').html(data);
});
It returns something along the lines of "Username/Password" are incorrect. Are the fields wrong? Is my approach fundamentally flawed? If so, how could a similar effect be achieved? Thanks!
Edit: Follow up question, if I send an ajax request and that page should create/set a cookie, what happens to that cookie? Does it just evaporate, or is it set for the browser?
I mean, there are some developer/other tools that let change variables in Javascript enviroment and so on. Consider I have an AJAX request. So could user, for instance, manipulate variables I send with AJAX request so that they see something they are not used to see? Or, for instance, manipulate value of <select> so that it is other, not like in options given. I hope you understand what I mean.
But how to implement AJAX in this case? Can client create his own request? So just send error back if request was not like it should be?
I send with AJAX request so that they see something they are not used to see?
YES!
You can't rely on client side scripting for security!
Check out:
Fiddler
Firebug
After your edit:
Can client create his own request?
Yes.
So just send error back if request was not like it should be?
Yes.
It's not "your" Javascript code. It's a bunch of characters you send to the client in the hopes that it will do something useful with it.
Javascript doesn't "work" by default. Everything that happens happens because the client did it. So yes, the client affects everything about the workings of your Javascript code.
Is there a difference between me using Javascript to redirect to URL + "?Querystring=value" versus using whatever mechanism ASP.NET uses?
If there is a difference, how can I make the rendered ASP.NET page be submitted to the same URL with a different query string by javascript?
If you want to do a post back just like a asp control like a asp:Button you can use the javascript functions included by the framework to do so:
__doPostBack('ControlIDOfEventYouWantToRaise','');
You can read more about the __doPostBack in this article:
Doing or Raising Postback using __doPostBack() function from Javascript in Asp.Net
Just doing a form.submit() will not be exactly the same as using __doPostBack.
To answer the first part of your question there is no difference doing a redirect if you are just doing a Response.Redirect as the will both do a GET. The difference is if you use a asp:Button control for instance, it will access your page first to handle the button (a post back) and then do a GET on the redirected page.
If you want to submit to the same URL (eg post your data) then you should use the __doPostBack method. If you don't require the data to be posted, then just do a redirect in javascript to the same URL with a modified query string (which will just do a basic GET) but your data will not be posted.
The only potential difference is that a querystring parameter is sent via GET, a form is (usually) sent by POST.
GET has a much smaller data limit as browsers have a max URL length (it varies)
You could use javascript to do a form.submit() which shoul emulate what ASP.Net does
I somewhat disagree with Basiclife's answer; if you have any code inside something like
if (IsPostBack) {
it's not going to be equivalent, ie the code is going to be executed if you're just setting the URL. Also, controls keep their state across postbacks but are freshly initialized if you're calling the URL again. This is due to ASP.NET trying to emulate a "normal" application, so the way to make sure a normal call and a postback have the same effect might result in "de-ASP.NET-ing" the entire page.
I'm not sure if what you want works. There probably is a way. But I heavily suspect there's a better way of doing this. If you get a postback for free, and can transmit data, why is it crucial that the data shows up in the URL, instead of being comfortably posted? I can see how you want a page to respond to a URL parameter, and how you might want to change the same parameter later on based on what's happening on that page, but since you always know you're posting back, you can eg override that URL parameter in that case, by something you're posting back. This doesn't sound so nice, but it might actually be less messy. Particularly since you seem to have a reason to not abandon the postback at all (otherwise you could just use a link, right?).
I'm looking for an easy way to call a bean's method that will take no parameters and return a string in JSF. The thing that I don't really need is that the method returns an action result and then uses the whole JSF life-cycle to do get me to another view. I need to do that from JavaScript so that I can put together some client-side parts of the application and going over the A4J part of RichFaces has brought me nothing so far.
So here's the scenario again in a step-by-step form:
from JS issue a GET on some address
on the server process that GET and return JSON or HTML (basically a string)
once the request is sent back to the client I want to be able to process it further with JS.
Thanks!
Use a4j:jsFunction and the data attribute.
So roughly you want something like:
<button onclick="callBackend();">Go</button>
<a4j:jsFunction name="callBackend" action="#{myBean.someMethod}" data="#{myBean.someString}" oncomplete="handleResponse(data);"/>
<script>
function handleResponse(response) {
alert(response);
}
</script>
Damo: can you explain why it might only work for the first time the method callBackend is executed? I'm experiencing a strange behavior that the first call succeeds and the next calls are just blocked. I see the server-side code being executed but some strange result is being sent back to the browser (something like the _viewstate and those kind of things).