Posting data at clients side localhost from server - javascript

I would like to POST some JSON data at clients side localhost from my Web Application.
I tried using localhost but this refers to the Server Side - Localhost. I also tried to use Javascript instead of PHP but this didn't work as well.
The goal I am trying to achieve is to be able to send some data to the clients localhost where an other desktop application will be listening to a specific port and then do some work (like printing a label) with the given data.
Code I tried:
(I am using XAMPP as a localhost test server)
$.ajax({
type: 'post',
url: 'localhost',
dataType: 'json',
data: 'json',
success: function(json) { ... },
error: function(error) { ... }
});

are you referring to a server and client within a local private network? If so, you can just specify the IP address and port of the client when sending the post request.
Ex:
$.ajax({
type: 'post',
url: '192.168.xxx.xxx:8080',
dataType: 'json',
data: 'json',
success: function(json) { ... },
error: function(error) { ... }
});

Related

SSL_ERROR_HANDSHAKE_FAILURE_ALERT with JQuery GET request

There is a server which provides a XML under a certain URL (for example: https://myxmlfile). For this server request is a p12 certificate with password needed which I installed in Firefox and on my machine. If I enter the URL in the webbrowser (https://myxmlfile), the xml will be shown.
Now I try to make a jQuery GET Request from my website (which is just running on my local machine at the moment). In that GET Request I want to get the same XML, but this doesn't work and I get SSL_ERROR_HANDSHAKE_FAILURE_ALERT.
I'm pretty new to certificates. So is it not enough to install the certificate in the browser? Do I have to send the certificate in the GET request or something like that?
This is the request:
$.ajax({
type: "GET",
url: 'https://myxmlfile',
data: 'subscriptionID=0011',
dataType: "xml" ,
async: false,
error: function(xhr, status, error){
console.log('Error: '+ xhr.status+ ' - '+ error);
console.log("error");
},
success: function(xml) {
console.log(xml);
}
});
Thanks for your help.

How to call RESTful from another server by JavaScript

I have a server A running a web application (in ASP.NET MVC 4), using JavaScript to call a REST API residing on server B.
JavaScript in Server A:
$.ajax({
type: "POST",
url: "http://serverB/api/Employee/Manpower",
contentType: "application/json; charset=utf-8",
data: convertTojsonWithData(REQHEAD(), EMPDETAIL()),
success: function (Result) {
//alert(data);
$("#txtOption").val(Result);
alert($("#txtOption").val());
}
});
When I run my project, I received nothing from the REST API on Server B. And when I changed my url to api/Employee/Manpower, the API endpoint becomes http://localhost:1111/api/Employee/Manpower.
Please guide me!
What is Server B's ip address ?
Try with Ip Address of server B.
for example call http://192.168.5.1:8080/api/Employee/Manpower

how to change the remote API path to localhost using javascript

I'm accessing the API URL which is located remotely[in US] using the browser. It works fine, displays the API link JSON data(valid json data). When i try to access the same using the AJAX call in the script, it fails with 401 unauthorized error even with valid credentials and key.
I got suggestion to make the path to the API server[remotely located] and configure port number to 81. Can you please suggest me how to configure this.
Script:
$.ajax({
cache: false,
type: "GET",
async: true,
dataType: "JSON",
username: "myemailid",
password: "mypassword",
url: "http://<remotehostipaddress>:82/JSON/Lookups/ComponentTypes",
data: { apiKey: "api key" },
success: function(results) { alert("Success!" },
error: function(xhr) { alert("Error!" }
});

Ajax request failing?

I am trying to retrieve json data from an api.
It keeps failing, but when I look in the Net tab of Firebug I can see that the GET request executed and returned the correct data. Am I doing something wrong or does anyone have tips on how to debug this?
Edit: Have changed to dataType json and error status code is 0
Thanks
$.ajax({
url: 'http://localhost:55894/api/Test/All',
data: {
format: 'json'
},
error: function () {
alert('Error');
},
dataType: 'jsonp',
success: function (data) {
alert('Ok');
},
type: 'GET'
});
From the info you provided the reason it is failing is because you dont have a cross domain access policy setup. Because you are using different ports to host the website and the API you are running into this issue. You can either setup a crossdomain.xml with the proper security settings or you can move both the API and the webserver to the same port.
Have a look at this for more info: http://en.wikipedia.org/wiki/Same-origin_policy
u can try this way:
$.ajax({
type: 'GET',
url: 'url api here',
beforeSend: function() {
},
success: function(data) {
},
error: function(xhr) { // if error occured
},
complete: function() {
},
dataType: 'json'
});
JSON and JSONP are different. If you are using JSONP, the server side must be prepared to support it. Doesn't look like you are using JSONP.
So just change dataType to 'json', as you are "trying to retrieve json data".

Communicate with an https server(SAP j2ee server) uising Ajax, jQuery and PhoneGap

I am very new to the phonegap platform, and i am on research about the issue below for the last two weeks.
I am working on a PhoneGap project for android which communicates with a remote https server(SAP j2ee server). I dont have any controll over server. The server gives data as SOAP response only in a secured way.
Now my problem is that i am not able to send a request from my application to the server. When i send a request i get either HTTP Status 'error' or a success message without any data.
Is there any alternative methods available for the same issue for android and blackberry?
Please go through the code below and please help me.
function soapRequest(){
$.mobile.showPageLoadingMsg();
var username=$("#username").val();
var password=$("#password").val();
var SOAP_Login='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:OrSVi"><soapenv:Header/><soapenv:Body><urn:getP><urn:userName>'+username+'</urn:userName></urn:getP></soapenv:Body></soapenv:Envelope>';
$.ajax({
url: URL_Login,
type: "POST",
dataType: "xml",
async:false,
cache: false,
crossDomain:true,
data: SOAP_Login,
complete: endSaveProduct,
beforeSend: function (xhr) {
xhr.setRequestHeader("SOAPTarget", "https://ex.example.com:443/OrSvi/Config1?style=document");
xhr.setRequestHeader ("Authorization", "Basic WFWeMjEyMDerebWrteTyyku==");
xhr.setRequestHeader("SOAPAction","urn:OrSviWsd/OrSVi_Document/getPRequest");
},
contentType: "text/xml;charset=UTF-8"
});
}

Categories

Resources