How to access web service from Javascript or PHP? - javascript

I want to access a web service from here in my web application. When the user chooses a species name, the service will inform if the name is valid, the author and year of the name, etc.
First I thought of using Javascript, since the information comes from the user. Then I saw cross-domain restrictions, so I wonder what is the best workaround here. According to this suggestion, I should be using a server-side workaround. But in that case, wouldn't it be easier to just use php curl functions?

Yes.
If you want to do this reliably, do it from a computer you control.
If you are using PHP already, than cURL is the usual HTTP client library.
That said, using PHP/cURL on your server is the server side workaround to cross-origin JS issues, not an alternative to using a workaround.

Try this ajax code.
$.ajax({
url: 'http://localhost/webservice.php?callback=',
type: "GET/POST",
data:{data:JSON.stringify([{action:'getUserData', userId: 1234}])},
dataType: "jsonp",
crossdomain: true
});
For more details refer this link HTML-PHP Webservice using the AJAX with crossDomain as TRUE.

Related

How to run a java program through a JavaScript page?

I have a java program to scan vehicle's number plate and i want to call this program through a JavaScript page i.e. When I click a button on my JavaScript page it should execute my java program . I know there are similar questions on stackoverflow, but none was clear enough for a beginner like me to understand. New to JavaScript, any help would be highly appreciated. Thank you in advance.
While the answer of "No" is technically correct based on the phrasing of the question. You may want to read up on AJAX. It is a way for javascript to make a request to your backend code (in this case Java).
Javascript is client side, meaning it is run by the user's browser. Java is running on your server. In order for the client side javascript to interact with the backend Java, you need to make a request to the server.
You can do it with AJAX.
Javascript is client side, meaning it is run by the user's browser. Java is running on your server. In order for the client side javascript to interact with the backend Java, you need to make a request to the server.
A simple example would be something like this
$.ajax({
type: 'POST',
url: 'http://localhost:8080/MyMethod',
data: JSON.stringify({"string" : "anything you want to send to your method"}),
contentType: "application/json",
error: function() {
alert("Failed");
},
success: function() {
alert("Success");
}
});
That depends on where you would like to run it on.
1.client side
The only method to get java codes running directly on client side, is to use a java applet. Write an applet,write your html properly, then you are all set.
Or, you may want a wasm/javascript compiler for java.
2.server side
you should setup a mechanism letting your frontend to raise the backend.
for frontend, you should be able to send certain requests. you can choose http request, aka XHR/AJAX, or, you can choose web socket. they are similar things.
For backend, if you let your httpd handle the very request, then you should have your httpd notify your code for that. The solution if different for different httpds.
If you want to handle the request directly, then you can just listen to the very port and do the regular things. You should be responsible for security issues.

jQuery AJAX not loading URL with hash

I have the following AJAX:
$.ajax({
cache: false,
type: 'GET',
url: 'preview.php?url=http://domain.com/Demo/MarsPlaybook/#/view-0',
...
However when I check the console for what was requested, the hash part is binned off...
The console just says: 'preview.php?url=http://domain.com/Demo/MarsPlaybook/'
Any ideas why this is happening? Or how I can stop it?
The hash fragment isn't sent to the server, and you cannot make it be. The hash is purely for the browser to maintain your location within the page.
You'll have to encode it as part of the query string.
The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the web server
-- http://en.wikipedia.org/wiki/Fragment_identifier
You may want to set the url to: 'preview.php?url=http://domain.com/Demo/MarsPlaybook/?view=0' then on the server/php side direct to 'preview.php?url=http://domain.com/Demo/MarsPlaybook/#/view-0'
However, without knowing a lot about what you're doing, It's hard to say the best answer for you.

How to hide the data in source code from the user

As a side project to learn Web Development, I'm writing a web app in Javascript that allows my fellow classmates type in our Class ID # to a search field. If they enter the correct Class ID, they will automatically be redirected to our Google Groups page. The only problem I'm seeing is that since I'm running multiple Google Groups for different classes that I'm taking, I don't know how to hide the javascript code.
Example in Pseudocode:
If (input === 12345){
redirect to (LinkToClass1GoogleGroupsPage.com)}
Else If (input === 12344){
redirect to (LinkToClass2GoogleGroupsPage.com)}
The problem here is if they right-click and view source code, they will clearly see what inputs I'm looking for. I'm new to Web Development and I would like to know what's the best way to implement something like this.
You cannot hide JavaScript code. If you have a secret, keep it on the server.
Anything on client-side environment is readable unless it is encrypted - what doesn't works with JavaScript. You can use a server-side environment to deal with that without leaving JavaScript with node.js, look this post.
Use an ajax request(jQuery or pure) to a node.js service or any other server-side language of your choice and keep those actions out of user's sight. This is safer, right and maybe only way to do that.
You cannot literally hide the data in JavaScript, unless you use a server-side language to redirect.
What you can do however is obfuscate your code, there are tools to help you do this.
http://javascriptobfuscator.com/
"LinkToClass2GoogleGroupsPage.com"
Results in
var _0x2ec6= ["\x4C\x69\x6E\x6B\x54\x6F\x43\x6C\x61\x73\x73\x32\x47\x6F\x6F\x67\x6C\x65\x47\x72\x6F\x75\x70\x73\x50\x61\x67\x65\x2E\x63\x6F\x6D"];_0x2ec6[0];
Probably the most secure way for this to be done would be to have a Server Side function that can be called via Ajax to return the link.
What type of Server Side code you use depends on your preferences.
For example ASP.NET Web Service, PHP, ASPX Web Methods.
Below is example Ajax Request Code using jQuery :
var o = new Object();
o.ID = input;
var x = JSON.stringify(o);
$.ajax({
url: 'SOME URL', //Path to the Server Side function (i.e. Php / ASPX Web Method / Web Service)
type: 'GET',
dataType: 'JSON',
contentType: 'application/json;charset=utf-8;',
data: x,
success: function (data) {
//Method returned without issue
redirect to (data.d)
//data is a JSON object that contains a "d" property, if your function returns a string then d will be the value of the string.
},
error: function (ajaxrequest) {
//Ajax call received an error.
}
})
This cannpt be done yet. HTML5 might have DRM implementations in the future but this will also depend on browsers opting in for this feature (Mozilla are against it for example).
Disable right click and ctrl button thats all you can do! :D

Update twitter status without prompting user using pure JavaScript

I have web application where people can login from twitter and based on their preference and DOM events I need update their status on twitter. I have a good idea how to do this on server side, but for this project I am not using any server side code, So how can I do this by just javascript, #anywhere twitter api and twitter intents are taking me to no where because they prompt user for submitting the tweet which I dont want.
A pure Javascript solution to consume twitter API is not possible without compromising your consumer secret key. Twitter API authenticates every request using a HMAC-SHA1 token, the SHA1 token is generated using the public/private key assigned by twitter to your api account. If you plan to generate this token using a pure javascript SHA1 implementation then it means you will be exposing your private key in the javascript code which anyone can look at.
Even though technically its possible (provided you can find a javascript library which implements SHA1), its not advisable.
FYI, jQuery.Ajax method does let you modify the headers of the ajax request by tapping in to the beforeSend(jqXHR, settings) method.
You should be able to do this with an AJAX POST request to the REST API. Documentation: https://dev.twitter.com/docs/api/1/post/statuses/update
You target the URL http://api.twitter.com/1/statuses/update.format, where format can be either xml or json, and reflects the format of the response. Required data is the status text, and there are several optional parameters which I won't list here. This only works for the currently authenticated user.
An (untested) example using jQuery:
var message = "This is a tweet, there are many like it but this one is mine";
$.ajax({
type: "POST",
url: "http://api.twitter.com/1/statuses/update.json",
data: "status="+message,
datatype: "json"
});

jQuery ajax load html from another server [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Load website into DIV
Hey all i am trying to find a way to call an external website and retreve its HTML so that i can check for something in it (wither the user entered a valid VIN # or not depending on what this website calls back)
This is the code i have:
$.ajaxSetup ({
cache: false
});
$("#load_callback").click(function(){
$.ajax({
url: 'http://www.google.com',
dataType: 'text',
success: function(data) {
alert(data);
}
});
});
It works but only if i have it pointed to my server (and running on my server also). Is there a way i can call an external website using jquery/ajax? I am aware of the Same origin policy but was hoping something like what i want to do did not fall into that catagory?
I cant even get an iFrames title? Really??? wow....
David
You need to use jsonp: http://en.wikipedia.org/wiki/JSON#JSONP
These links should explain it:
http://www.remysharp.com/2007/10/08/what-is-jsonp
http://www.ibm.com/developerworks/library/wa-aj-jsonp1-
The other option you have is to write your own server-side proxy to it, i.e. have a page/controller/handler on your server that passes your request through and returns the result. It won't be as fast as going direct, and it will increase your site's traffic, but it will get you around the security problem.
Stick to simple things, if you want to load websites, be simple and go with iframe. If you want to make request use PHP or similar. For example you can use curl method to make request to another websites through PHP. Or even simpler, setup a form to request to another web server.

Categories