Is there AJAX support?

Asked by TheSpy

I have a problem, it seems that on the page i have to send an AJAX request in order to send a message. Does the Esmska support XMLHttpRequest(); ? Here is a send method I have done. Maybe you can see the problem.

function send() {
    var re, match, match2, content, params, postData, ret

    // check that login and password supplied
    if (LOGIN.length == 0 || PASSWORD.length == 0) {
        EXEC.setErrorMessage(EXEC.ERROR_WRONG_AUTH)
        return false
    }

    // start page
    logout()

    postData = ["username", LOGIN, "password", PASSWORD, "autologon", "no", "logon", "true"]
    content = EXEC.postURL("http://www.coolsms.dk/Logon.html", [], postData)

    // login sucesesfull?
    re = /<input name="number" class="text" type="text">/
    match = re.exec(content)
    if (undefined != match) {
        logout()
        EXEC.setErrorMessage(EXEC.ERROR_WRONG_AUTH)
        return false
    }

    // send message
    SendMSG(NUMBER,MESSAGE);

    return true;
}

var sendRequest;
var statusRequest;
var checkmsgid;

function createRequest(){
  var request = null;
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        alert('Din browser er ikke underst ttet. Opdater venligst din browser.');
      }
    }
  }
  if(request != null){
    return request;
  }
}

function SendMSG(rc,message){
  sendRequest = createRequest();
  sendRequest.open("POST",'http://www.coolsms.dk/ajax/sendFriSMS.php',true);
  sendRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  sendRequest.send('recipient=' + escape(rc).replace(/\+/g,"%2B") + '&msg=' + encodeURI(message).replace(/\+/g,"%2B").replace(/\&/g,"%26"));
}

function logout() {
 EXEC.getURL("http://www.coolsms.dk/Logaf.html", [])
}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

And there is the javascript code from the gateway.

http://www.coolsms.dk/javascript/frisms.js

Question information

Language:
English Edit question
Status:
Solved
For:
Esmska Edit question
Assignee:
No assignee Edit question
Solved by:
Kamil Páral
Solved:
Last query:
Last reply:
Revision history for this message
Kamil Páral (kamil.paral) said :
#1

Esmska HTTP client library certainly doesn't support any JavaScript, just basic HTTP. Of course you can evaluate some simple JavaScript in the operator script (it is JavaScript script itself), but you can't access any HTML DOM. Also I don't expect some classes common for browsers like XMLHttpRequest to be available there.

But from my experience most of the gateways use JavaScript only for checking input values in forms, but don't require it for actual sending. Try to disable JavaScript in your browser and try to use the gateway. If it works, then you don't need anything special in the gateway script. Also if I am not mistaken the XMLHttpRequest is just a standard HTTP request and response, therefore can be emulated by posting data to particular URL.

From looking into your script, I see posting some data to http://www.coolsms.dk/ajax/sendFriSMS.php. So just don't do it with javascript objects but with standard Esmska methods: EXEC.postURL(...). You just have to have the parameters in the correct format. Esmska is already converting post data to x-www-form-urlencoded format, so maybe it is not needed at all (just "recipient=NUMBER&msg=MESSAGE"). You can intercept the data with WireShark and look if the data is encoded in the same way from Esmska and from browser.

Revision history for this message
TheSpy (sourness) said :
#2

Thanks for info. I have checked without JS and it doesn't work. It seems to be a little more sophisticated. When a send button is pressed, XMLHttpRequest is initiated and values from the form are passed to sendFriSMS.php. Here is data from WhireShark:

This one sends message, I send id through the browser:
http://img503.imageshack.us/img503/8116/coolsms.png

And this one doesn't, through Esmska.
http://img509.imageshack.us/img509/6546/coolsms1.png

Here is the method:

function send() {
    var re, match, match2, content, params, postData, ret

    // check that login and password supplied
    if (LOGIN.length == 0 || PASSWORD.length == 0) {
        EXEC.setErrorMessage(EXEC.ERROR_WRONG_AUTH)
        return false
    }

    // start page
    logout()

    postData = ["username", LOGIN, "password", PASSWORD, "autologon", "no", "logon", "true"]
    content = EXEC.postURL("http://www.coolsms.dk/Logon.html", [], postData)

    postData = ["recipient","+4561960386","msg", "test"]
    content = EXEC.postURL("http://www.coolsms.dk/ajax/sendFriSMS.php", [], postData)

    return true;
}

It seems that sendFriSMS.php checks for a referrer and as far as I understand it is impossible to change any header value (also referrer) with JavaScript.
Any suggestions?:)

Revision history for this message
TheSpy (sourness) said :
#3

And it is true. I have disabled Referrers and I couldn't send message:) So it is necessary to set the right referrer, just don't know how.

Revision history for this message
Best Kamil Páral (kamil.paral) said :
#4

That's not a problem, Esmska provides a solution to this :)

http://ripper.profitux.cz/esmska/javadoc/esmska/transfer/OperatorExecutor.html#setReferer(java.lang.String)

EXEC.setReferer(...);

When you set it, it is used for all further connections (so you might need to set it to different value or reset it before making another request).

Revision history for this message
TheSpy (sourness) said :
#5

Thanks Kamil Páral, that solved my question.