1

I'm working on a script that posts simple commands to a web server embedded on a device. I'm not sure exactly what the server is (some flavor of Apache on embedded Linux, I believe) and it shouldn't matter for what I'm asking.

The code on the server is reacting differently to a URL sent from jQuery's $.ajax() function and the exact same URL sent from Chrome. I'm trying to find a way to figure out what Chrome is doing with this request that jQuery is not in order to make my javascript work.

Details:

When I post to the device to send it a command, I do it like this:

$.ajax({
    type: "GET",
    url: urlPrefix+'/setsetting/'+name+'='+value,
    success: function(r) {
        //command was sent
    },
    error: function(e) {
            //some http error
    }
});

When I make a request in which the value does not contain spaces, the server does its job properly. However, when I make a request where the value does contain spaces, the server doesn't do it's job. So there is obviously a problem with how the code on the server is handling spaces in the URL, right? Here's the weird thing: When I take a URL constructed with this code and put it directly into my browser window, the server does its job even if there is a space in the value. So this URL sent from my script:

http://[ip]/setsetting/setting1=value 1

does not work, but if I plug that same url into my browser (Chrome), it does work. Chrome will change the space to %20. I've tried formatting the value in my code to replace spaces with %20 which doesn't help. I've tried adding the contentType attribute of $.ajax() to other things and none of that helped.

Edit: I just noticed that the error function of $.ajax() is firing. e.responseText is an html page that says "400 - Bad Request". Hmmm?

Ross
  • 1,313
  • 4
  • 16
  • 24
AnalogWeapon
  • 550
  • 1
  • 4
  • 16
  • 2
    So basically you're just asking how does the browser format the URL and what does jQuery do differently. You can look at the HTTP headers to see the exact HTTP GET requests of either direct navigation or the jQuery AJAX calls. [here's how.](http://stackoverflow.com/q/4423061/1313143) – MarioDS Nov 03 '12 at 22:42
  • Thank you! I think this get's me one step closer to figuring this out. – AnalogWeapon Nov 03 '12 at 23:04
  • is url on same domain, subdomain and port? – charlietfl Nov 04 '12 at 00:06

2 Answers2

0

What about just using the encodeURIComponent function? It will automatically convert all special symbols to url friendly format.

encodeURI(url) is used for the whole url and encodeURIComponent is used when you compose your url "http://example/?someValue=" + encodeURIComponent(someValue)

Kirill Ivlev
  • 12,310
  • 5
  • 27
  • 31
  • If I do that to the whole url, it encodes the / characters also, so no love. But when I do that to just value (url: '/setsetting/'+name+'='+encodeURIComponent(value),) I get no love either. – AnalogWeapon Nov 03 '12 at 23:03
-1

Try this:

$.ajax({
    type: "GET",
    url: urlPrefix+'/setsetting/?'+name+'='+value, //Note the use of '?'
    data: 
    success: function(r) {
        //command was sent
    },
    error: function(e) {
            //some http error
    }
});

Here we use the ? to better delimitate where your query strings parameters begin.

Nelson
  • 49,283
  • 8
  • 68
  • 81
  • and what is '?' supposed to do? Just saying you should include that in your answer... If that's the querystring delimiter, the remote code will not process the request correctly. – MarioDS Nov 03 '12 at 22:48
  • To explain my downvote, see my previous comment. Even if it does work, it will not do anything about the spaces... – MarioDS Nov 03 '12 at 22:53
  • Mario, you are wrong, you don't need to encodeURIComponent() the url field of `$.ajax()` function, because jQuery already does that automatically. – Nelson Nov 03 '12 at 22:57
  • Right. I'm struggling with the way that server code reacts to the request, not the syntax of the request specifically. If I put the ? in the url it won't work from my script or from the browser. – AnalogWeapon Nov 03 '12 at 22:58
  • @user1530115 I don't understand what was your result of using the `?` – Nelson Nov 03 '12 at 22:59