How to call RESTful from another server by JavaScript - 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

Related

Posting data at clients side localhost from server

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) { ... }
});

Creating an API in yii for cross domain communication

I am planning to create a Restful API in yii for cross domain communication. As what I have researched, cross domain communication can only be done via jsonp and my implementation is as follows:
UsersController:: actionCrossDomain
public function actionCrossDomain(){
$this->layout=false;
$data['data']['User']['name']= 'Johny';
$this->_sendResponse(200, CJSON::encode($data),'application/json');
}
UsersController::_sendResponse methdod it is the same as you can see in :Click here
On another server that i configured using virtual host, I am invoking the above method via ajax:
$( document ).ready(function() {
$.ajax({
type: "POST",
dataType: "jsonp",
url:'http:'http//uxaserver.local/alpha2/app/users/crossDomain' ,
data: null,
processData: false,
crossDomain: true,
contentType: "application/json",
success: function (data) {
console.log("done");
},
error: function (request, status, error) {
console.log(request);
}
});
});
The issue is my firebug is is complaining that : SyntaxError: invalid label
My requirement is such because I am helping my client's to do some customized analytic to his other websites and I need to put a script in his web pages of different domains so that the analytic data is recorded in the main server. I understand that I need to use the rest interface to communicate thus I am taking this approach. I am not sure if I have taken the right approach and please advice. I want to make it in a way where a client has an api key and can communicate with the api provided by me.
Is there any other approach to do this? Rather than jsonp?
As i see this string contents error
url:'http:'http//uxaserver.local/alpha2_uxarmy/app/users/crossDomain' ,
Should be
url:'http//uxaserver.local/alpha2_uxarmy/app/users/crossDomain' ,

How to connect to a SQL CE database from JavaScript running in Internet Explorer?

I am developing a Browser Plugin for which I am storing data locally in SQLCE Database .
But I have to display saved data in browser in form of a webpage , for this my webpage needs to connect to local database and fetch the values . I am planning to use javascript to do the same . But haven't got any success till now .
Can anyone point me to the right direction whether it is possible or if not possible any other
way I can do that .
You cannot access a SQLCE database from JavaScript, but you can publish it through OData protocol using ASP.NET WebAPI or WCF Data Services.
If you use IE10 (that supports EcmaScript5) you can follow this tutorial
you cannot connect dirctly from javascrip
but you can make a [web method] in asp connect to your database
and you can call this method from java scrip by ajax call
ex:
in cs file :
[WebMethod]
public static string myMethod(long ID)
{
////you can in method to connect to any database
return _result;
}
in javascript file :
$.ajax({
type: "POST",
url: "mypage.aspx/myMethod",
data: "{ID:'" + ID + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function(result) {
}
, error: function(error) {
}
});

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"
});
}

Ajax request using Javascript

I have to make ajax request for mobile web app, I used the following code
$.ajax({
type: type, //GET or POST or PUT or DELETE verb
url: requestURL, // Location of the service
// contentType: "application/x-www-form-urlencoded", // content type sent to server
dataType: "xml", //Expected data format from server
processData: false, //True or False
success: successCallback, //On Successfull service call
error: serviceFailed// When Service call fails
});
But it failed with error "Error: Access is denied. I am calling .aspx service from javascript
You cannot submit AJAX requests to domains other than the one serving the web page. If it is a sub domain causing the problems, you can use this code to resolve it.
document.domain = location.host.replace(/^.*?([^.]+\.[^.]+)$/g,'$1');

Categories

Resources