How to send data to web services?


#1

Hi, i have a problem in sending data to web services.
From a web page using only normal jquery so it works:

$.ajax({
			url: "../ws/ars-services.php?method=getProductPosition",
			method: "post",
			data: {"param": "{token:'asdasdasdasdasdasd', product_code:'123'}"},
			success: function(result){
				
				if (result.result) {
					//
				} else {
					//
				}
				
			}
		});

I tried to replicate it as follows, but without success:

$http({
	    method: 'POST',
	    url: "http://localhost:8888/ws/ars-services.php?method=getProductPosition",
	    data: {"param":"{'token':'asdasdasdasdasd', 'product_code':'123'}",
	    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
	})
	.success(function(data) {
		//
	})
	.error(function(data) {
		//
	});

works:

data: "param=%7Btoken%3A'asdasdasdasdasd'%2C+product_code%3A'123'%7D"

I hope for your help :slight_smile:


#2

whats the repsonse?
any errors or something like that?


#3

The response is a json, which is not the correct result since the data submitted are not interpreted correctly by the web service and then the service responds error.


#4

and you are sure that your request is a post request because the name of your api-function is getProductPosition?


#5

I found this solution, which seems to work but I have no idea why :slight_smile:

transformRequest: function(obj) {
    var str = [];
    for(var p in obj)
    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
    return str.join("&");
},