Blank response on cross domain POST Request - javascript

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

Related

How to get a Jupyter Notebook file in a custom extension

I am trying to write a Jupyter Notebook extension in which I also have to create a submit button which should post the current notebook file to the external server.
The trouble I am facing is how can I get the notebook file object and then post it. I am not sure if there is a way to get the orginal ipynb file and post it.
I can get a browser URL for example "http://localhost:8888/notebooks/Untitled.ipynb", so I figured I could send a get request to this URL and then post it but the get request obviously sends an HTML file of a notebook which is opened in the browser.
Here is my code
var that = Jupyter.menubar;
var browserURL = Jupyter.notebook.container[0].baseURI;
var request = new XMLHttpRequest();
request.open('GET', browserURL, true); // Send get request to browser URL
request.onload = function() {
var reader = new FileReader();
reader.readAsDataURL(request.response);
reader.onload = function(e){
var notebookFile = e.target.result; // returned File
$.ajax({
url : 'http://example.com/hello.php',
type : "POST",
dataType: "json",
crossDomain: true,
data: {
'file': notebookFile,
'msg': "Hello"
},
success: function(data) {
console.log("Success");
},
error: function(xhr, status, error) {
console.log("error");
}
});
};
};
request.send();
My question is that how can I get the notebook file that I am working on, so I can post it to the server.
Thank you

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.

Sending data to PHP using AJAX

I have searched my problem before posting this question, but failed to find a solution.
I need to send a json string to php file but unable to do so, can some one please help with my problems below: I'm new to php and jquery and is struggling, Need your cooperation please.
I have a function that captures data on the text file:
function updateVal() {
var node_list = document.getElementsByTagName('input');
var c = 0;
var fieldName = [];
var fieldText = []
var ID = [];
for (var i = 0; i < node_list.length; i++) {
var node = node_list[i];
if (node.getAttribute('type') == 'text') {
fieldName[c] = node.name;
fieldText[c] = node.value;
ID[c] = node.id;
c++;
}
}
var postData = {
fieldName: fieldName,
fieldText: fieldText,
ID: ID
};
var dataString = JSON.stringify(postData);
console.log(JSON.stringify(postData));
$.ajax({
type: "POST",
dataType: "json",
url: "update.php",
data: {myData:postData}
})
//return JSON.stringify(postData);
}
My update.php is like this:
<?php
$json = $_POST['json'];
$result = json_decode($json);
echo $result;
echo $_POST['myData']);?>
On loading this: I'm getting the following error:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Moreover, I'm not sure if the data being sent to php or not.
Can experts pls validate.
The 500 (internal server error) means something went wrong on the server's side. So check the apache error log for more details
you may find apache log here /var/log/apache2/
On client side (javascript code):
data: JSON.stringify(postData)
On server side (PHP code):
json_decode($_POST["data"])
u commented the closing braces of the function with the return statement.
change this :
//return JSON.stringify(postData);}
to:
//return JSON.stringify(postData);
}
Also :
data: JSON.stringify(postData),
IN update.php
$json = $_POST['myData'];
$result = json_decode($myData);
var_dump($result);
Comment from #Tushar helped me resolving the issue
contentType: 'application/x-www-form-urlencoded; charset=UTF-8', data: {json: JSON.stringify(postData)
sent Ajax request to php Page to Get data
$.ajax({ method: "POST", // it may be Get url: "some.php", //page where you sent request data: { name: "John", location: "Boston" } //attibutes you want to take on that page }) .done(function( msg ) { // sucessfull reponse alert( "Data Saved: " + msg ); });

HTTP 405 api jquery ajax php

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.

Merging Javascript AJAX requests

I have a performance problem when loading a website over the network stored on an internal server. The performance issue is down to the multiple AJAX requests made in my Javascript file constantly trying to access an XML file and then getting an Image from a XML node or a Name from an XML node.
Is there a way I can merge the AJAX requests together to reduce the amount of requests from the client device to the XML file stored on the server.
My code is as below:
function getimage(getID,XMLImageID)
{
$("#" + getID).empty();
$.ajax({
type: "GET",
url: "XML/XML.xml",
dataType: "xml",
success: function(xml){
$(xml).find(XMLImageID).each(function(){
var image = $(this).find("image[href]").attr("href");
$('#'+ getID).append($("<img"+" class="+"Timages"+" src=\"" +image+"\"/>"));
});
},
error: function() {
alert("(getImage) An error occurred while processing XML file. Reasons could be: \n The XML cant be located. \n The XML is being used by another program. \n The XML is trying to parse incompatible characters. \n The XML syntax/tags are incorrect.");
}
});
}
function getdept(getid,XMLID)
{
var scriptTag = document.scripts[document.scripts.length - 1];
var parentTag = scriptTag.parentNode;
getid = parentTag.id;
$.ajax({
type: "GET",
url: "XML/XML.xml",
dataType: "xml",
success: function(xml){
$(xml).find(XMLID).each(function(){
var dept = $(this).find('Dept').text();
var id = $(this).attr("id");
var sName = $(this).find('Name').text();
$("#"+getid).append('<div class="Team_People" onmouseover="area_hover1(this);area_hover2(this)" href="#p'+id+'">'+dept+'<br />'+sName+'</div>');
});
},
error: function() {
alert("(getHierPeopleDept)An error occurred while processing XML file. Reasons could be: \n The XML cant be located. \n The XML is being used by another program. \n The XML is trying to parse incompatible characters. \n The XML syntax/tags are incorrect.");
}
});
}
The AJAX response uses a simplified code below, I just need to merge the code above to make it more neater rather than having multiple ajax requests. Not sure if this is possible (whether adding multiple parameters would work)?
$.ajax({
type: "GET",
url: "XML/XML.xml",
dataType: "xml",
success: function(xml){
$(xml).find(XMLID).each(function(){
//Get something from XML node
});
},
});
}
If anyone could guide me in the right direction that would be much appreciated!
Thanks
Regards
AJ
Create a global XML variable and query that with your function calls...
var XML = null;
function fetchXML(){
$.ajax({
type: "GET",
url: "XML/XML.xml",
dataType: "xml",
success: function(data){
XML = data;
},
error:function(){ alert('something went wrong');})
}
function getImage(id) {
$(XML).find(id).each(){ ... };
}
funcition getDept(id) {
$(XML).find(id).each(){ ... };
}

Categories

Resources