HTTP 405 api jquery ajax php - javascript

i am trying to add member on myewellness.com
i am using jquery and php on a wordpress website, i have to collect the user's details and add him to myewellness.com
this is the link to their api documentation:
http://www.mediafire.com/view/pr6s7isht9ihdao/API-Services-Documentation-v2.0-1.pdf
but i am getting this on chrome:
OPTIONS https://api.myewellness.com/api/v1/services/membership/create send # jquery.js?ver=1.11.2
(index):1 XMLHttpRequest cannot load https://api.myewellness.com/api/v1/services/membership/create. Response for preflight has invalid HTTP status code 405
(index):1509 Failed: [object Object]
and CORS not enabled error on fire fox,
here is my js:
jQuery.noConflict();
(function( $ ) {
$(function() {
$("#register_form").submit(function(){
console.log("Wellness API Initiated!");
var api_user_name = $("#user_name").val();
var api_user_email = $("#user_email").val();
var api_payment_first_name = $("#payment_first_name");
var api_payment_last_name = $("#payment_last_name");
var api_user_password = $("#user_password").val();
var api_client_address_one = $("#client_address_one").val();
var api_client_address_two = $("#client_address_two").val();
var api_client_city = $("#client_city").val();
var api_client_state = $("#client_state").val();
var api_client_zip = $("#client_zip").val();
var api_client_gender = $("#client_gender").val();
var api_client_home_phone = $("#client_home_phone").val();
var api_client_cell_phone = $("#client_cell_phone").val();
var api_client_work_phone = $("#client_work_phone").val();
var api_client_birth_date = $("#client_birth_date").val();
$.ajax({
type: "POST",
url: "https://api.MYEWELLNESS.com/api/v1/services/membership/create",
/*headers: {
'Authorization':'Basic Vml0YWxhbGVydA==:MTI5OTky',
'Content-Type':'application/json'
},*/
data: "user_name="+api_user_name+"&password="+api_user_password+"&first_name="+api_payment_first_name+"&last_name="+api_payment_last_name+"&address1="+api_client_address_one+"&address2="+api_client_address_two+"&city="+api_client_city+"&state="+api_client_state+"&country=US&zip="+api_client_zip+"&email="+api_user_email+"&phone="+api_client_home_phone+"&work_phone="+api_client_work_phone+"&cell_phone="+api_client_cell_phone+"&gender="+api_client_gender+"&birthdate="+api_client_birth_date,
datatype: "json",
beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic " +"Vml0YWxhbGVydA==:MTI5OTky"); },
success: function(msg){
console.log("Success: "+msg);
},
error: function(error){
console.log("Failed: "+error);
}
});
console.log("API Fire Complete!");
return false;
});
});
})(jQuery);

Their server isn't giving you permission to instruct other people's browsers to access their API.
You shouldn't want to do that anyway - otherwise you will be handing over your API key to all your visitors!
Make the API requests from your server, using PHP, instead. You'll probably want to look at the cURL library to do that.

Related

Issues with make jQuery ajax call to a rest api

I am trying to use jquery ajax to make a post request that returns some response but it does not seem to work properly. Sometimes it works after a long wait, other times it does not work at all.This is my code.
<script type="text/javascript">
const id = $('#auth').val();
$("#set").click(function(){
$('.spinner-grow').show();
$.post("https://ravesandboxapi.flutterwave.com/v2/gpx/transactions/escrow/settle",
{
id: id,
secret_key: "FLWSECK-25*******************0628-X"
},
function(data, status){
alert("Data: " + data + "\nStatus: " + status);
$('.spinner-grow').hide();
});
});
</script>
what might be the issue here?
The following is a complete example of a AJAX post is handled via jQuery. Just sharing it for reference.
var url = "api/path/to/your/controller";
var data = {};
data.id = id;
data.secret = 'your-secret';
$.ajax({
type: 'POST',
url: url,
data: data,
success: function (resultObject) {
console.log(resultObject);
},
error: function (err) {
console.log('An error occured');
}
});
If you are getting a 404 error, better check the path to the API. You can use a tool like Postman to validate your requests and then try implementing the same in code.
Also make sure that there is no CORS issue as from your code we cannot understand if your post is triggered from the same domain.

How to make post request using javascript

I am trying to make a POST request in javascript. But response is not getting.
http://xecutehr.com/api/HRMSService.svc/PostAttendance?input={"Header":{"CompanyId":"MTF","LicenceKey":"MTF-4525"},"Body":{"E_Code":"01330","AttendanceDateTime":"2016-04-27T13:00:30","Mode":"I","DeviceId":"MTF1330"}}
This is the url format and this is the code I have written:
if(valid){
var values = form.serializeObject();
var e_code = values.staff_code.substring(3);
var device_id = values.staff_code;
var date_time = datetime;
var url = "http://xecutehr.com/api/HRMSService.svc/PostAttendance?input=";
$.ajax({
url:url,
headers: {
"CompanyId":"MTF",
"LicenceKey":"MTF-160427-RHGB-4525"
},
type: "POST",
data:{
"E_Code":"0"+e_code,
"AttendanceDateTime":date_time,
"Mode":"I",
"DeviceId":device_id
},
datatype: "jsonp",
success: function(data) {
alert(response);
}
});
}
I tried a lot I am missing some basics in in . How can I handle this? suggest some solutions or links
You can't have jsonp and do a POST.
A jsonp request loads as a <script>.
E.g.
request: /url?callback=something&E_Code=0something
The response will be something like:
something({E_Code: '0something'})
Where something() is your ajax response handler.

Safari Extension not geting session Info

I create simple safari extension which used simple js file where i send request on my own server and check user session is set or not. In my extension code when i send Ajax call to my own server it get other information but can't get user session while user is logged in.Here is my code:
var storeUrl = window.location.href;
$(document).ready(function()
{
var store = storeUrl.replace("http://",'');
store = store.replace("/",'');
jQuery.ajax({
url: 'https://www.karmora.com/checkstore/' + store,
context: document.body,
error: function(data, transport) {
},
success: function(data) {
var storeData = jQuery.parseJSON(data);
console.info(storeData);
}
});
});

Blank response on cross domain POST Request

I'll Start with a basic introduction of what i am trying to achieve, basically i am integrating my web application with an ERP(Tally ERP 9) they provide an HTTP/XML API Which is listening on port 9002 and my application is on port 8085 so the problem starts here, when I'am trying to make an ajax call for posting some data i'am getting a blank response,even after getting a success, And the request is performing the task perfectly but the only issue is that i am getting a blank response Following is my js
function postTallyData()
{
var XMLData='<ENVELOPE>...</ENVELOPE>';
var Tallyurl = 'http://localhost:9002';
$.ajax({
url: Tallyurl,
type: "POST",
data: XMLData,
dataType: "text",
converters:{"* text": window.String, "text html": true, "text json": jQuery.parseJSON, "text xml": jQuery.parseXML},
crossdomain: true,
complete:function(responseXML){
alert(responseXML);
var xml = responseXML,
xmlDoc = $.parseXML(xml),
$xml = $(xmlDoc),
$CREATED = $xml.find("CREATED");
$ALTERED = $xml.find("ALTERED");
$LASTVCHID = $xml.find("LASTVCHID");
$LASTMID = $xml.find("LASTMID");
$COMBINED = $xml.find("COMBINED");
$IGNORED = $xml.find("IGNORED");
$ERRORS = $xml.find("ERRORS");
if(parseInt($ERRORS.text())>0)
{
$LINEERROR = $xml.find("LINEERROR");
jAlert($LINEERROR.text());
}
if(parseInt($CREATED.text())>0)
{
$LINEERROR = $xml.find("LINEERROR");
jAlert("Voucher : "+$LASTVCHID.text()+" Created in Tally");
}
}
});
}
and here is the firebug screenshot

Cross-domain calling a WCF Service

I have a WCF service, this is a method which I'd to call:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
double?[] GetPoints(string tourname);
I checked by WCF Test Client, it works fine
So I need to call this method from html page. It should work from other computer that is cross-domain.
I wrote somethig using jQuery 1-6-2.min.js:
var varType;
var varUrl;
var varData;
var varContentType;
var varDataType;
var varProcessData;
function CallService() {
alert("CallService");
$.ajax({
type : varType, //GET or POST or PUT or DELETE verb
url : varUrl, // Location of the service
data : varData, //Data sent to server
contentType : varContentType, // content type sent to server
dataType : varDataType, //Expected data format from server
processdata : varProcessData, //True or False
success : function(msg) {//On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
}
function Start() {
varType = "POST";
varUrl = "http://localhost:1592/TourService.svc/GetPoints";
varData = '{"tourname ":"customname"}';
varContentType = "application/json; charset=utf-8";
varDataType = "json";
varProcessData = true;
CallService();
}
function ServiceSucceeded(result) {
alert("ServiceSucceeded");
alert(result);
}
function ServiceFailed(result) {
alert('Service call failed: ' + result.status + ' ' + result.statusText);
varType = null;
varUrl = null;
varData = null;
varContentType = null;
varDataType = null;
varProcessData = null;
}
However ServiceFailed function is invoked with message "Service call failed 0 error"
How to make a cross-domain calling of WCF service?(using jQuery or not)
Thanks
Basically you need to be using jSONP instead of jSON:
Using jQuery & JSONP for cross-domain AJAX with WCF services
I'll provide a summary shortly in case this link disappears.
Also see the jQuery documentation for more about how to use jSONP, jQuery & AJAX
.net framework 4 for wcf has json call back inbuilt now ,
I think jquery1.5 onwards they added the following option , crossDomain , try the below code
$.ajax({
type: "POST",
dataType: "html",
crossDomain: true,
url: "http://www.domain.com/page.aspx",
error: function() {
alert("error");
},
success: function(msg){
alert(msg );
}
});

Categories

Resources