Announcement

Collapse
No announcement yet.

Using import/config through javascript

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using import/config through javascript

    Hello it's me again ^^'

    I'm trying to make my own home page for my module and I want to allow user to upload a file.txt to configure the module.
    My issue of comprehension is about the way to send data.

    With Postam, I can do it and then I tried to learn about the parameters of postam.



    I don't understand how works the body parameters, especially the key and value !

  • #2
    Well sorry I understood the parameters but I still have a problem ^^'

    So I'm using JavaScript codes to upload a text file wich is in my example :

    Code:
    <?xml version="1.0" standalone="yes"?>
    <!-- Automatically generated XML -->
    <!DOCTYPE configrecord [
       <!ELEMENT configrecord (configgroup+)>
       <!ELEMENT configgroup (configitem+)>
       <!ELEMENT configitem (value+)>
       <!ELEMENT value (#PCDATA)>
       <!ATTLIST configrecord version CDATA #IMPLIED>
       <!ATTLIST configgroup name CDATA #IMPLIED>
       <!ATTLIST configgroup instance CDATA #IMPLIED>
       <!ATTLIST configitem name CDATA #IMPLIED>
       <!ATTLIST configitem instance CDATA #IMPLIED>
       <!ATTLIST value name CDATA #IMPLIED>
    ]>
    <configrecord version = "0.1.0.1">
       <configgroup name = "Access Point" instance = "ap0">
          <configitem name = "SSID">
             <value>MSL_MS4</value>
          </configitem>
       </configgroup>
    </configrecord>
    As you can see, I want to change the name of the SSID.

    Then the first step I upload the file and I configure it whith the good key :

    Code:
    var f = document.getElementById('XML'); // R?cup?ration de l'ID de l'input.
    
    	var files = XML.files;
    	
    	var formData = new FormData();
    	for (var i = 0; i < files.length; i++) {
    	var file = files[i];
    
    	
    
      // Add the file to the request.
    	formData.append('configrecord', file);
    	}
    	import_config(formData);
    And finally I make my post request with the good header :

    Code:
    var xhr=new XMLHttpRequest(); //Cr?ation d'un objet XMLHttpRequest 
    	var url= document.location.host; //On r?cup?re l'IP de notre module
    	
    	xhr.open("POST", "http://"+url+"/import/config", true);// On initialise une requ?te POST .False=Fonctionnement SYNCHRONE, on attends la r?ponse du serveur.
    	xhr.setRequestHeader("Content-Type", "multipart/form-data","Authorization"+'Basic '+ window.btoa(unescape(encodeURIComponent("admin" + ':' + "mslaboratoires"))));//On remplit l'en t?te avec le login et le mot de passe
    	
    	xhr.onreadystatechange = function() { 
    	   if(this.readyState == 4){ 
    			alert(this.responseText);
    			}
    	} 
    	
    	xhr.send(fichier_config);//Envois de la requ?te.Key="configrecord" et en value notre fichier de configuration
    And my issue is I don't have any response from the module ! Not even an error response ! And the SSID doesn't change !
    Last edited by BC_FR; 07-21-2015, 10:23 AM.

    Comment


    • #3
      If you run the Chrome Developer tools (Ctrl-Shift-I), do you see an error message when you do the send?

      Comment


      • #4
        Yes I have "net::ERR_EMPTY_RESPONSE".
        I tried your example wich is in datasheet (but it's in html) and it works !
        So the problem comes from my code... ><

        Comment


        • #5
          Try simplifying the request and see which lines are causing the trouble. For example, you could remove the setRequestHeader. The authentication shouldn't be needed since the user is already authenticated if they are accessing the page on the xPico Wi-Fi's webserver.

          Another option is to also remove the URL from the open, and just pass the "/import/config" relative path. Not sure that would have an effect, but some things to try to simplify the code.

          Mariano

          Comment


          • #6
            Oh here is my new code for the test :

            Code:
            	var xhr=new XMLHttpRequest(); //Cr?ation d'un objet XMLHttpRequest 
            	var url= document.location.host; //On r?cup?re l'IP de notre module
            
            	var configrecord;
            	xhr.open("POST", "/import/config", true);// On initialise une requ?te POST .False=Fonctionnement SYNCHRONE, on attends la r?ponse du serveur.
            	xhr.setRequestHeader("Content-Type", "multipart/form-data");//On remplit l'en t?te avec le login et le mot de passe
            	
            	xhr.onreadystatechange = function() { 
            	   if(this.readyState == 4){ 
            			alert(this.responseText);
            			}
            	} 
            	//Envois de la requ?te.Ici aucune donn?e n'est n?cessaire
            	
            	xhr.send(configrecord);
            The problem seems come from my content type: when i put a wrong type the server reply with error 400 and when I put the right type, I have no response from the server (my dialog box is empty at the end).
            It is strange because with Postman in the same configuration, I have a reply .
            I have the same error on Chrome, Firefox and IE. ><
            Last edited by BC_FR; 07-27-2015, 12:11 PM.

            Comment


            • #7
              You still need to create a FormData object and add the configrecord key. See how the setLineConfig function in our xpwLib.js does it:

              https://github.com/Lantronix/xpwJSlib

              Comment


              • #8
                I agree but what I don't understand is why the response is empty ! The server should give me an XML file with the error, like I have on postman :

                Code:
                <?xml version="1.0" standalone="yes"?>
                <!-- Automatically generated XML -->
                <!DOCTYPE function [
                   
                <!ELEMENT function (return)>
                <!ELEMENT return (result,message+)>
                <!ELEMENT result (#PCDATA)>
                <!ELEMENT message (#PCDATA)>
                <!ATTLIST function version CDATA #IMPLIED>
                ]>
                <function version = "0.1.0.0">
                    <return>
                        <message>ERROR: Mismatched expected XML Prolog at line 1.</message>
                        <message>ERROR: XML import failed.</message>
                        <result>Failed</result>
                    </return>
                </function>

                Comment


                • #9
                  By the way it works now, thx for your support

                  Comment

                  Working...
                  X