How to make nusoap server with nusoap lib, and call using Javascript - javascript

I make nusoap server.php file with nusoap-0.9.5 php in my localhost,
now in index.html file, how can I call "getmessage" function using the javascript, such as:
Below code not working.. please help..
<?php //---server.php--begin---(php)
require_once("lib/nusoap.php"); //liberary
$server = new nusoap_server();
$server->configureWSDL("myService","urn:service1"); // configuare WSDL
function getmessage($message){
return "Welcome ".$message;
}
$server->register(
"getmessage",
array("name"=>"xsd:string"), //input
array("sum"=>"xsd:string") //output
);
//http listener
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA: '';
$server->service($HTTP_RAW_POST_DATA);
//---server.php--end---
?>
-------------------Javascript SOAP Client--------------------
<!---index.html--begin---(javascript html)--->
<SCRIPT language='javascript' src='soapclient.js'></SCRIPT>
<SCRIPT>
var url = "http://localhost/server.php?wsdl";
function getmessage()
{
var pl = new SOAPClientParameters();
pl.add("name", document.frmDemo.txtName.value);
SOAPClient.invoke(url, "getmessage", pl, true, getmessage_callBack);
}
function getmessage_callBack(r)
{
alert(r);
}
</SCRIPT>
<form id="frmDemo" name="frmDemo" action="" method="post">
<input name="txtName" id="txtName" value="Matteo" type="text">
<input type="button" value="click here" name="button" onClick="getmessage();"> <br>
</form>
<!---index.html--end--->

Javascript don't have stable library for soap, you must make it or use customized simple lib by any developers.. for example go to this link and do it:
https://www.ibm.com/developerworks/library/ws-wsajax/

Related

How to send Basic HTML Form data to Spring Boot controller?

How to send HTML Form request i.e; value of 'input' to #PostMapping in controller without creating any POJO class or jsp?
<body>
<form method="post" id="form">
<input type ="text"/>
</form>
<button type="button" id="button2" >Submit2</button>
<script src="script2.js"></script>
</body>
Script2.js
var select = document.getElementById('form');
document.getElementById("button2").onclick = function(){
var value = select.value
window.location.href = "/posting";
};
MyController.java
#PostMapping(value="/posting")
public String po() {
return "hello";
}
Specify the name for your input element:
<body>
<form method="post" id="form">
<input type ="text" name="someName"/>
</form>
<button type="button" id="button2" >Submit2</button>
<script src="script2.js"></script>
</body>
In your script create HTTPRequest and send data:
var select = document.getElementById('form');
document.getElementById("button2").onclick = function(){
var data = new FormData(select);
var req = new XMLHttpRequest();
req.open("POST", "/posting");
req.send(data);
};
You may also want to send JSON data. Convert data to JSON and send json variable
var object = {};
data.forEach(function(value, key){
object[key] = value;
});
var json = JSON.stringify(object);
req.send(json);
And in java controller you have to specify that the method parameter is a request body. Something like this:
#PostMapping("/posting")
public String po(#RequestBody String someName) {
return "hello" + someName;
}
Check if controller class has #RequestMapping("/") annotation
you can use #RequestParam see RequestParam in Spring
add action attribute to form and make button in form (not needed js file because send direct by form):
<body>
<form action="/posting" method="post" id="form">
<input type ="text" name="someName"/>
<button type="submit" id="button2" >Submit2</button>
</form>
</body>
and change in controller :
#PostMapping("/posting")
public String po(#RequestParam("someName") String someName) {
return "hello" + someName;
}
but if need use js file you can use XMLHttpRequest see XMLHttpRequest api or ajax see jQuery.ajax or fetch fetch api

how to use form data every second with ajax

I'm very confused. Can anyone help me?
Here is the code:
index.php =>
<html>
<head>
<script src="jquery-1.2.6.min.js"></script>
<script>
function chk(){
var name = $('#name').val();
$.ajax({
type:"post",
url:"test.php",
data:{name:name},
cache:false,
success:function(html){
$('#msg').html(html);
}
});
return false;
}
setInterval (function chk() , 1000);
</script>
</head>
<body>
<form>
<input type="text" id="name">
<input type="submit" value="submit" onclick="return chk();return false;">
</form>
<p id="msg">Loading</p>
</body>
</html>
and test.php =>
<?php
$name = $_POST['name'];// $name is constant for each request that submited from ajax($name is a parameter)
$api -> new server_data();
$results = $api ->show($name);
print_r($result);//This data updates from server in every second
=======================================================================
I'm trying to connect to an API, but in that API, data changes every second and I have to see these changes. On the other hand, I have to set default values with the form.
But the problem is here that each time the form submits, the posted value in test.php will be null, and the ajax request will not work.
Can anyone help me?
Try adding a name attribute to your name input box element.
<input type="text" id="name" name="name">
Your PHP $_POST['name'] variable is looking for an input element with the html attribute name="name", whereas you only had an html attribute id="name".
Source: PHP: Dealing with Forms

Why I lost the filename value when I click the submit button

I have made a form to pass xml files on a server. Although when it correctly sends the file to the server, it seems that the pages reloads and the value of the file name is lost and my javascript code returns nothing.
However if I remove the line
<input type="submit" />
from the code below it keeps the filename in the variable x but it doesn't send the file to the server.
I want to send the file on the server and keep its name into a variable in order to use in any other javascript functions.
<?php
$uploads_dir='/web/stud/external/scomvs2/epicurus';
if(isset($_FILES['xml_file1'])){
$errors= array();
$file_name = $_FILES['xml_file1']['name'];
$file_size =$_FILES['xml_file1']['size'];
$file_tmp =$_FILES['xml_file1']['tmp_name'];
$file_type=$_FILES['xml_file1']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['xml_file1']['name'])));
$expensions= array("xml","XML");
if(in_array($file_ext,$expensions)== false){
$errors[]="extension not allowed, please choose an xml file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"$uploads_dir/$file_name");
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" id="uploading" name="xml_file1"/>
<input type="submit" />
</form>
<button type="button" onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("uploading").value;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
You can add in our Javascript part the following:
<script>
function myFunction() {
var x = <?=json_encode($file_name)?>;
document.getElementById("demo").textContent = x;
}
</script>
Now when in Javascript you call that function, the demo element will show the file name. Note that it is better to use textContent instead of innerHTML, although with filenames you don't risk to have special characters that HTML would interpret (<>&).
Now you should also make sure that $file_name is always defined. So at the top of your PHP code, add:
$file_name = '';
Instead of injecting the file name in your Javascript code, you could also let it be injected directly in the demo tag, like this:
<p id="demo"><?=htmlspecialchars($file_name)?></p>
Then you don't need Javascript to fill it in, if that is all you wanted to do.

Code is not working

This is the code:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function nameNotAva()
{
alert("Name already exists.");
//some code
}
function nameCheck()
{
var username = document.getElementById('uname').value;
var url = "http://rscript.org/lookup.php?type=namecheck&name=";
var curl = url + username;
$.ajax({
url : curl,
type : 'GET',
success : function(urlOutput){
if(urlOutput.contains('NAMECHECK: NOTAVALIBLE')){
nameNotAva();
}
}
});
}
</script>
</head>
<body>
<input class="textBox" id="uname" type="text" maxlength="15" required/>
<input type="button" onclick="nameCheck()" value="Submit">
</body>
</html>
The problem is there in the $.ajax part. I know this because when i put alert(curl); just before the ajax part, it displays the correct URL. If you want the complete info that what i am trying to do then goto- How to check output of a created URL?
I am using jquery library of the latest version.
You haven't included the jQuery library so $ is undefined and the script will error on the Ajax part.
In general, if you have a problem with some JavaScript: Open the JS console in your browser and pay attention to any error messages.
Update after comments suggest that the code in the question was incomplete:
This is the library i am using- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js" type="text/javascript" />
Script elements are not defined as empty. The end tag is required.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js">
</script>
http://jsfiddle.net/ksJEj/
Change the input type:
<input type="submit" value="Submit"/>
include this in your <head></head> tags:
HTML
<script type="text/javascript" src="ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
and this too:
HTML/JavaScript
<script type="text/javascript">
$(document).ready(function{
function nameNotAva() {
alert("Name already exists.");
//some code
}
$('input[type="submit"]').click(function (event) {
event.preventDefault();
var username = $('#uname').val();
$.get("http://rscript.org/lookup.php", {
'type': 'namecheck',
'name': username
}, function (urlOutput) {
if ($(urlOutput).contains('NAMECHECK: NOTAVALIBLE') == true) nameNotAva();
else alert("woohoo");
});
});
});
</script>

POST JSON to Jersey Service

I am trying to post some json data to REST web service implemented with Jersey framework. I am not using JAXB or jquery but only javascript.
I verified that formed json is correct but in spite of setting content type "application/json", on server it is received as "application/x-www-form-urlencoded".
Here is my code:
<html>
<head>
<script type="text/javascript">
function DisplayFormValues()
{
var str = {};
var elem = document.getElementById('frmMain').elements;
//alert(elem.length);
for(var i = 0; i < elem.length-1; i++)
{
str[elem[i].name] = elem[i].value;
}
document.getElementById('lblValues').innerHTML = str;
var json = JSON.stringify(str);
// construct an HTTP request
var xhr = new XMLHttpRequest();
xhr.open(document.getElementById('frmMain').method,
document.getElementById('frmMain').action);
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("Content-Length",json.length);
xhr.setRequestHeader('Accept', 'application/json');
//alert(json);
// send the collected data as JSON
xhr.send(json);
xhr.onloadend = function() {
// done
}
}
</script>
</head>
<body>
<form id="frmMain" name="frmMain" action="/JerseyTest/rest/postUser"
method="post">
<input name="firstName" value="harry" /> <input name="lastName"
value="tester" /> <input name="toEmail" value="testtest#test.com" />
<br /> <input type="submit" value="Test"
onclick="DisplayFormValues();" />
</form>
<hr />
<div id="lblValues"></div>
</body>
</html>
On the server side:
package com.example.jersey.test;
import javax.ws.rs.*;
#Path("/postUser")
public class JsonTest {
#POST
#Consumes("application/json")
#Produces(MediaType.TEXT_PLAIN)
public String pingPong(String json) {
return "Answer is "+ json;
}
}
I am new to web development and not sure on what I am missing in above code.
I am answering my own question for those who may visit this later. The code above is correct and works well except the fact that the url is been hit twice. First time, for submit button's default action and then as per our script by XMLHttpRequest.
This I came to know after I checked the headers in Httpfox which showed error as NS_BINDING_ABORTED.
After changing the input type to button from submit, all is working fine.

Categories

Resources