I am making an AJAX API request from a PhoneGap project to a web server. But the server is not responding.
Here is my code:
jQuery(document).ready(function($) {
$("#submit").click(function(){
var email = $("#email").val();
var password = $("#password").val();
var baseURL='http://mydomainname.com/myWebProject/';
$.ajax({
url:baseURL+'CustomerAuth/loginCheck',
method:"GET",
dataType: 'json',
data: {email:email, password:password},
success:function(data){
alert("style");
}
});
});
});
My web project is CodeIgniter 3x
I have disabled CSRF protection
I have added these code in config.xml:
<access origin="*" />
<access origin="http://multidimensionsystems.com" />
<allow-navigation href="*"/>
And here is the code of my method of web project:
public function loginCheck() {
echo "success";
}
Thanks in advance for your help!
Related
Am trying pull data from .net core web api using ajax call am getting the below error
Failed to load http://localhost:8085/api/menu: Response for preflight has invalid HTTP status code 404.
but am getting data sucessfully from postman.
am trying below code for ajax
$(document).ready(function(){
$.ajax({
url: 'http:/localhost:8085/api/menu',
type: 'GET',
dataType: 'json',
data: '{ }',
success: function (data) {
console.log(data);
},
failure: function (data) {
alert(data.responseText);
}, //End of AJAX failure function
error: function (data) {
alert(data.responseText);
}
});
});
I have added below tag in web.config file
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
Please suggest your thoughts.
Thanx in advance.
you can define your policy in Startup file as a part of ConfigureServices method
for example:
{
services.AddCors(c => {
c.AddPolicy("policyName", p => {
p.AllowAnyOrigin().AllowAnyMethod();
});
});
}
and then in Configure method
app.UseCors("policyName");
When loading a Dynamics CRM form with a HTML web resource I get the below error from the Chrome browser console.
https://xxxx.api.crm6.dynamics.com/api/data/v8.2/<custom entity>. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://.crm6.dynamics.com' is therefore not allowed access. The response had HTTP status code 401.
<script type="text/javascript">
var clientUrl = "https://xxxx.api.crm6.dynamics.com/api/data/v8.2/"
function GetAccounts() {
var url = clientUrl + "accounts"
$.ajax({
method: "GET",
url: url,
async: false,
beforeSend: getAccountsBeforeSendCallback,
fail: getAccountsFailCallback,
done: getSavingGoalsDoneCallback,
success: getAccountsSuccessCallback
});
}
function getAccountsBeforeSendCallback(jqXHR, settings) {
debugger
jqXHR.setRequestHeader("OData-MaxVersion", "4.0");
jqXHR.setRequestHeader("OData-Version", "4.0");
jqXHR.setRequestHeader("Accept", "application/json");
jqXHR.setRequestHeader("Content-Type", "application/json; charset=utf-8");
}
</script>
It seems you're doing a request to another domain. Are you sure your clientUrl is on same domain?
var clientUrl = "https://xxxx.api.crm6.dynamics.com/api/data/v8.2/";
var rightUrl = window.Xrm.Page.context.getClientUrl() + "/api/data/v8.2";
if (clientUrl !== rightUrl) {
console.log("You will get the 'Access-Control-Allow-Origin' error!");
}
A lot of people have trouble with the $.ajax and XmlHttpRequest stuff. Luckily there are libraries, which will take care for this. Example of crm-sdk, which will do same as your code:
<script type="text/javascript" src="CRMSDK.js"></script>
<script type="text/javascript">
var WebAPI = window.CRMSDK.WebAPI;
WebAPI.retrieveMultiple("account").then(function (data) {
getAccountsSuccessCallback(data); //this is your method.
});
</script>
I am trying to create simple NodeJS server using express framework.
and at client site, I wanted to fetch data using ajax call but it's not working
My Server side code
var express = require('express');
var app = express();
function sendJson(req, res){
console.log('User connected');
var jsonEx = '{ "result" :{"name" : "sachin", "surname" : "Tendulkar"}}';
res.type('application/json');
res.send(JSON.stringify(jsonEx));
}
app.use("/",express.static(__dirname));
app.get("/",sendJson);
app.listen(3000);
And client side code : which is the file index.html
$(document).ready(function(){
//Make ajax call to fetch data
$.ajax({
url: "http://localhost:3000/",
type: "GET",
dataType: 'json',
success: function(resp){
console.log(resp);
console.log("Hello");
}
});
});
but nothing happens after running the example.
console shows no data.
I enter below url in browser to run this
http://localhost:3000/index.html
what is problem with the code ?
Thank you,
Sachin
Express's app.use and app.get will act the same if you specify a path, but will resolve in order. So in this case, all your requests are rendering the index page. See this post(Routing with express.js - Cannot GET Error) Try changing the json data to another route like this:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
//Make ajax call to fetch data
$.ajax({
url: "http://localhost:3000/data",
type: "GET",
dataType: 'json',
success: function(resp){
console.log(resp);
console.log("Hello");
}
});
});
</script>
</head>
</html>
and
var express = require('express');
var app = express();
function sendJson(req, res){
console.log('User connected');
var jsonEx = '{ "result" :{"name" : "sachin", "surname" : "Tendulkar"}}';
res.type('application/json');
res.send(JSON.stringify(jsonEx));
}
app.use("/",express.static(__dirname));
app.get("/data",sendJson); // defining data as the get endpoint instead of root
app.listen(3000);
I have developed android app using phonegap. Now I am trying to use that code for developing windows phone 8 app. I have successfully set up the Visual Studio Express environment and made Cordova Template. After that I have created the project and inserted my assets into that project.
It shown me my app UI perfectly and events, page navigation are working fine. But it is not able to make ajax http connection. Do I need enable any thing in SDK or Project Directory.
When am I trying this , I am getting alert that 'Connectivity Failure'
This is my sample js code for making http connection
$.ajax({
beforeSend: function() { $.mobile.showPageLoadingMsg("a","Processing !!! Please wait "); },
complete: function() { $.mobile.hidePageLoadingMsg(); },
type: 'POST',
url:'http://192.168.12.175/Receiver.php',
data: reqObj ,
success: function(data){
responseString=data;
}
error: function(data){
//get the status code
alert('Connectivity Failure');
} });
use this one
$.mobile.showPageLoadingMsg("a","Processing !!! Please wait ");
formData = {
param1: param1
}
$.ajax({
type: 'POST',
contentType: 'application/json',
url: "http://192.168.12.175/Receiver.php",
dataType: "json",
data: formData,
success: function(data) {
$.mobile.hidePageLoadingMsg();
},
error: function(data) {
alert('Connectivity Failure');
}
});
Adding
<feature name="http://api.phonegap.com/1.0/network"/>
in config.xml fixed the issue for me.
This is adding the <Capability Name="ID_CAP_NETWORKING" /> into WMAppManifest.xml file when build is generated
I already worked with web API and had a lot of problems like posting multiple parameters. I upgraded to WebApi2 to use routing attributes and now have problems like:
"message":"The requested resource does not support http method 'DELETE'."
I spent all day searching Stack Overflow and the web to solve the problem:
Removed webdav
In http protocol allow all get,put,post,delete
Added the [HTTPDelete] attribute
Added name="ExtensionlessUrlHandler-Integrated-4.0" path="*."
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0"
Searched Google for the necessary help here
Can someone please guide me on this?
I had the same problem. Adding the below code to your web.config should fix the problem under the system.webserver section:
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/>
I had the same problem, because my controller was like this:
[HttpDelete]
public HttpResponseMessage Delete(string Id)
{
....
}
And on the client I used the send the ID as request body:
var url = '/api/upload';
var data = JSON.stringify({ 'Id': id }); // <-- In the body
$.ajax({
url: url,
type: 'DELETE',
data: data, // <-- in the body
contentType: 'application/json'
})
When I changed the client to use it as URL parameter instead it worked:
var url = '/api/upload/' + id; // <- In the URL
$.ajax({
url: url,
type: 'DELETE',
contentType: 'application/json'
});
The JavaScript code for the DELETE verb must be like this:
$.ajax({
**url: "/api/SomeController/" + id,**
type: "DELETE",
dataType: "json",
success: function(data, statusText) {
alert(data);
},
error: function(request, textStatus, error) {
alert(error);
debugger;
}
});
Do NOT use something like this,
...
data: {id:id}
...
as when you are using the POST method.