Send informations with AJAX, error "mycustompage.htm?aspxerrorpath=" - javascript

I use Javascript and AJAX to send some informations in a .NET MVC project. The code below works on my local machine but not on my server.
var str = "/Nouvelle_Fiche" + "/" + id_fiche + "/" + id_process + "/" + type_process + "/" + id_impact + "/" + id_auteur + "/" + id_situation + "/" + questionnaires;
xmlhttp.open("POST", str, true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send();
I have an error "404 Not Found" with the line "xmlhttp.send();" because it add to my link "mycustompage.htm?aspxerrorpath=" so i have :
"mycustompage.htm?aspxerrorpath=/ Nouvelle_Fiche/17/13766/I/43/1162/1/2_"
Can you help me please ? Thank you !

The main points MVC controller doesn't understand what you are trying to call.
I believe your route config looks like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{process}/{typeprocess}/{impact}/{auteur}/{situation}/{questionnaires}",
defaults: new { controller = "Home", action = "Nouvelle_Fiche", id = UrlParameter.Optional,
process = UrlParameter.Optional, typeprocess = UrlParameter.Optional,
impact = UrlParameter.Optional, auteur = UrlParameter.Optional,
situation = UrlParameter.Optional, questionnaires = UrlParameter.Optional}
);
Assuming that Controller name is: "Home" and action is "Nouvelle_Fiche" which take 7 parameters
then your request should be like this:
xmlhttp.open("POST", #Url.Action("Home", "Nouvelle_Fiche", new { id = id_fiche,
process = id_process , typeprocess = type_process,
impact = id_impact, auteur = id_auteur,
situation = id_situation, questionnaires = questionnaires }), true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send();

Related

How to parse XML soap response using Javascript

I'm using Javascript to call a SOAP webservice. Using firebug, I can see that the request is successful, and I see the XML SOAP response.
How can I display the response on the webpage? Or even better - how can I display a single node within the XML SOAP response? I thought maybe I can use XPath - but it doesn't seem to be working.
Here is my code:
<html>
<head>
<title>SOAP Client Test</title>
<script type="text/javascript">
function soap() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'https://mysoapurl.com', true);
// build SOAP request
var sr =
'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">' +
'<s:Header> ' +
'<USERNAME xmlns="http://www.tempuri.org">MyUsername</USERNAME>' +
'<PASSWORD xmlns="http://www.tempuri.org">MyPassword</PASSWORD>' +
'</s:Header>' +
'<s:Body>' +
'<GetData>Foo</GetData>' +
'</s:Body>' +
'</s:Envelope>';
// Send the POST request
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.setRequestHeader('SOAPAction', 'http://tempuri.org/MySoapActionURL');
xmlhttp.send(sr);
// send request
// ...
// This XPath query should get us the <GetResponse> element from the SOAP XML Response
var query = "//ns1:GetResponse[1]";
// This object defines the namespaces used in the query
var namespaceMapping = {
ns1: "http://tempuri.org/", // SOAP namespace
diffgr: "urn:schemas-microsoft-com" // the service-specific namespace
};
// Extract the <GetResponse> element from the response document
var responseNode=XML.getNode(XMLHttpRequest.responseXML, query, namespaceMapping);
return responseNode;
}
window.onload = soap;
</script>
</head>
<body>
</body>
<html>
Any help is greatly appreciated. Thanks for looking.
You can use the evaluate method on the responseXML:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'https://mysoapurl.com', true);
// build SOAP request
var sr =
'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">' +
'<s:Header> ' +
'<USERNAME xmlns="http://www.tempuri.org">MyUsername</USERNAME>' +
'<PASSWORD xmlns="http://www.tempuri.org">MyPassword</PASSWORD>' +
'</s:Header>' +
'<s:Body>' +
'<GetData>Foo</GetData>' +
'</s:Body>' +
'</s:Envelope>';
// Send the POST request
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.setRequestHeader('SOAPAction', 'http://tempuri.org/MySoapActionURL');
xmlhttp.onload = function(evt) {
var doc = this.responseXML;
var namespaceMapping = {
ns1: "http://tempuri.org/",
diffgr: "urn:schemas-microsoft-com"
};
var node = doc.evaluate('//ns1:GetResponse[1]', doc,
function (prefix) {
return namespaceMapping[prefix];
},
XPathResult.FIRST_ORDERED_NODE_TYPE,
null).singleNodeValue;
if (node != null) {
// now access node.textContent respectively run further queries on node
}
};
xmlhttp.send(sr);

send japanese character through XMLHttpRequest

I have to send the japanese name trough XMLHttpRequest. but it displays encoding problem... my tpl page is in utf-8 charset.
here is my code.
function getFormData(dno,rno) {
var name = document.getElementById("f_nickname").value;
var digNo = dno;
var resNo = rno;
var strVal = digNo + "-" + resNo;
stp.push(strVal);
var xhr = new XMLHttpRequest();
if (!xhr) return false;
var url = 'ajax.php' + '?prc=' + 'diagnoses' + '&name=' + name + '&diagres=' + stp;
xhr.open('POST', url, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(stp);
return true;
}
i have passing url like this:
http://crp.com/ajax.php?prc=diagnoses&name=大阪&diagres=0-0,1-3,2-2,3-3,4-3,5-2
but it displays in Ajax.php like
http://crp.com/ajax.php?prc=diagnoses&name=ƒsƒU&diagres=0-0,1-3,2-2,3-3,4-3,5-2
tried in many ways... How to solve?
thanks in advance...
Use encodeURIComponent for such parameters in url.
var url = 'ajax.php' + '?prc=' + 'diagnoses' + '&name=' + encodeURIComponent(name) + '&diagres=' + stp;
It will result in url like:
http://crp.com/ajax.php?prc=diagnoses&name=%E5%A4%A7%E9%98%AA&diagres=0-0,1-3,2-2,3-3,4-3,5-2
And webserver will handle it properly.
Try adding:
xmlHttp.setRequestHeader('Content-type', 'text/xml;charset=utf-8');
Use:
encodeURIComponent(name) instead of just name

How do I pull phoneNumber from Linkedin Javascript API array

I appreciate any help with those familiar with the LinkedIn JavaScript API. Question: How do I pull in the phone number and email from the field array. In my example, posted to a php script, both phoneNumber and emailAddress are undefined when set to $_POST[''];:
<script type="text/javascript">
//Runs when the JavaScript framework is loaded
function onLinkedInLoad() {
IN.UI.Authorize().params({"scope":["r_fullprofile", "r_emailaddress", "r_contactinfo"]}).place();
IN.Event.on(IN, "auth", onLinkedInAuth);
}
//Runs when the viewer has authenticated
function onLinkedInAuth() {
IN.API.Profile("me").fields("id,firstName,lastName,phoneNumbers,emailAddress,positions").result(displayProfiles);
}
// 2. Runs when the Profile() API call returns successfully
function displayProfiles(profiles) {
var p = profiles.values[0];
var phone = profiles.values[0].phoneNumbers;
for (var i in p.positions.values) {
var pos = p.positions.values[i];
if (pos.isCurrent)
var company = pos.company.name;
}
//AJAX call to pass back vars to server
var http = new XMLHttpRequest();
var postdata= "id=" + p.id + "&fName=" + p.firstName + "&lName=" + p.lastName + "&phone=" + phone + "&email1=" + p.emailAddress + "&company=" + company;
http.open("POST", "../inc/linkedin.php", true);
use fields like this http://developer.linkedin.com/documents/profile-fields
Try this...
IN.API.Profile("me")
.fields('id,email-address,first-name,last-name,date-of-birth,phone-numbers,positions,num-connections')
.result(displayProfiles)
.error(displayErrors);

xhr response: connection closed

I wrote a Flickr search engine that makes a call to either a public feed or a FlickrApi depending on a selected drop down box.
examples of the JSONP function calls that are returned:
a) jsonFlickrApi({"photos":{"page":1, "pages":201, "perpage":100, "total":"20042", "photo":[{"id":"5101738723"...
b) jsonFlickrFeed({ "title": "Recent Uploads tagged red","link": "http://www.flickr.com/photos/tags/red/","description": "", ....
the strange thing is that in my local install (xampp) both work fine and i get images back BUT when i host the exact same code on the above domain then the jsonFlickrApi doesn't work. What i notice (by looking at Firebug) is that for the jsonFlickrApi the response Header says Connection close
Also, Firebug doesn't show me a Response tab when i submit a request to the jsonFlickrApi
here is the code:
function makeCall(uri)
{
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = callback;
xmlhttp.open("GET", "jsonget.php?url="+uri, true);
xmlhttp.send();
}
function jsonFlickrApi(response)
{
var data= response.photos.photo ;
var output = "";
output += "<img src=http://farm" + data[4].farm + ".static.flickr.com/" + data[1].server + "/" + data[4].id + "_" + data[4].secret + ".jpg>";
document.getElementById("cell-0").innerHTML = output ;
}
//Public Feed
function jsonFlickrFeed(response)
{
var data= response.items[0].media.m ;
alert(data);
var output = "";
output += "<img src=" + data+ ">";
document.getElementById("cell-0").innerHTML = output ;
}
function callback()
{
//console.log("Ready State: " + xmlhttp.readyState + "\nStatus" + xmlhttp.status);
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
var jsonResponse = xmlhttp.responseText;
jsonResponse = eval(jsonResponse);
}
}
examples of calls:
a)
http://flickr.com/services/rest/?method=flickr.photos.search&api_key=75564008a468bf8a284dc94bbd176dd8&tags=red&content_type=1&is_getty=true&text=red&format=json&timestamp=1339189838017
b)
http://api.flickr.com/services/feeds/photos_public.gne?tags=red&format=json&timestamp=1339190039407
Question: why does my connection close? why is it working on localhost and not on the actual domain?
Looking at the HTTP response headers of
http://flickr.com/services/rest/?method=flickr.photos.search&api_key=75564008a468bf8a284dc94bbd176dd8&tags=red&content_type=1&is_getty=true&text=red&format=json&timestamp=1339189838017
I get a 302 with location
http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=75564008a468bf8a284dc94bbd176dd8&tags=red&content_type=1&is_getty=true&text=red&format=json&timestamp=1339189838017
So, what flicker wants to tell you is "use www.flicker.com instead of flicker.com!". With this URL I get content.

window.location + manipulating string url from window.location.href

I'm trying to get my facebook graph oauth setup using javascript's window location methods.
Here is my code so far:
function fbLog() {
var clientID = '11502353444318540727';
var redirecturi = 'http://google.com';
var clientSecret = '6987d02323442423231f8b9da767b060e9';
var codeURI;
$('#fbLogin').click(function() {
window.location = 'https://graph.facebook.com/oauth/authorize?client_id=' + clientID + '&display=touch&redirect_uri='+ redirecturi + '&type=user_agent';
codeURI = window.location.href;
codeURI.split('=');
console.log(codeURI);
codeURI = codeURI.split('=');
codeURI = codeURI[1];
console.log(codeURI);
codeURI = codeURI.split('#');
codeURI = codeURI[0];
console.log('mega fun');
window.open = 'https://graph.facebook.com/oauth/access_token?client_id=' + clientID + '&redirect_uri=' + redirecturi + '&client_secret=' + clientSecret + '&code=' + codeURI
});
}
when I set codeURI to window.location.href, is that original call back page dead?
I'm super confused. Have I switched to a different window when I try to get the codeURI?
You have to assume that as soon as you set window.location, the browser will load a new page and all code on the current page will stop running. Don't try to do anything afterwards.
As far as I can tell, all you need here is to do things in a different order.

Categories

Resources