0

I'm trying to exchange data via http with a HUAWEI E3272 usb modem. With linux I had some success using curl:

curl -X GET "http://192.168.1.1/api/device/information" -H "__RequestVerificationToken: $(curl -s -X GET http://192.168.1.1/api/webserver/token | grep token  | cut -d '>' -f2 | cut -d '<' -f1)" -H "Content-Type: text/xml"

Getting the right response:

<?xml version="1.0" encoding="UTF-8"?>
<response>
<DeviceName>E3272</DeviceName>
<SerialNumber>S7CBY14A17006941</SerialNumber>
<Imei>862600021252600</Imei>
<Imsi>724051120127678</Imsi>
<Iccid>89550536110015952771</Iccid>
<Msisdn></Msisdn>
<HardwareVersion>CH1E3272SM</HardwareVersion>
<SoftwareVersion>22.470.09.00.150</SoftwareVersion>
<WebUIVersion>13.100.11.00.150</WebUIVersion>
<MacAddress1>00:0D:87:8E:4B:AC</MacAddress1>
<MacAddress2></MacAddress2>
<ProductFamily>LTE</ProductFamily>
<Classify>hilink</Classify>
</response>

But I need to do it with jquery. I also tried 100 times, but I missing something that I dont know what it is. I need to pass a token via some header, before calling the method. I know that "g_requestVerificationToken" is getting values properly, because i debuged it.

This is my script:

var g_requestVerificationToken = '';
var headers = {};
  $.ajax({
  type: 'GET',
  url: "http://192.168.1.1/api/webserver/token",
  crossDomain: true,
  dataType: 'xml',
  success: function(data) {
  g_requestVerificationToken = (data).find("response").find("token").text();
  headers['__RequestVerificationToken'] = g_requestVerificationToken;
    $.ajax({
    type: 'GET',
    async: false,
    headers: headers,
    url: "http://192.168.1.1/api/device/information",
    crossDomain: true,
    ContentType: 'text/xml',
    success: function(data) {
    console.log(data);
    }});
  }});

Returning this error (beacause I couldn't pass the token session via Header):

<?xml version="1.0" encoding="UTF-8"?>
<error>
<code>125001</code>
<message></message>
</error>

And with it I'm getting these browse errors:

errors log

I'm using allow control allow origin plugin from Chrome Webstore to prevent some CORS errors.

The thing is: In linux via curl I'm able to properly communicate with the modem, but with jQuery not.

Can anyone give some help?

Thanks!!

dsicari
  • 590
  • 6
  • 13
  • well, what goes wrong with the jQuery version? You forgot to tell us that. What errors and/or unexpected behaviour is occurring? My first thought is a CORS error, which is something you're unlikely to be able to overcome. If the page where the ajax call is made is not on the same IP address as the server being called (i.e. http://192.168.1.1) then CORS is required. But I'd be surprised if a modem bothered to send CORS headers in the response. I may be wrong, but that's the first thing I'd check. It should be easy to spot such an error in your browser console. – ADyson Sep 04 '17 at 21:08
  • I edited the post and added some log and http response from linux and jquery. To prevent CORS error I'm using "allow control allow origin" plugin. Its not a professional way, but it works for me. Thanks!! – dsicari Sep 05 '17 at 11:32

1 Answers1

0

As far as I can see, your CORS setting makes jQuery do a pre-flight query via HTTP OPTIONS call (instead of a direct HTTP GET). Your server is telling that it has not implemented the OPTIONS method, hence returning HTTP 501 and jQuery throws in the towel at that point.

I'd look into if you can avoid the CORS hassle altogether - alternatively implement HTTP OPTIONS to return the appropriate CORS header on your server side.

jlaitio
  • 1,878
  • 12
  • 13