0

first of all i am on developing a mobile app using ionic framework

in one factory i need to talk to a server side which is not in my domain (cross domain request)

i used this code and it never works for me

$http.get({
    url: 'http://test.wilson34.com/test.php',

    data: {
        "name": "peter"
    }
}).
success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
    console.log('Success' + data);
    alert(data)
}).
error(function(data, status, headers, config) {

    console.log('Status : ' + status);

});

but when do it using jQuery it works like charm

jQuery.ajax({
    url: 'http://test.wilson34.com/test.php',

    data: {
        "name": "peter"
    }
}).
success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
    console.log('Success' + data);
    alert(data)
}).
error(function(data, status, headers, config) {

    console.log('Status : ' + status);

});

please help i will not use jQuery in my project , any suggestions t do it using $http in angularJs

thanks in advance

3
  • try to set $httpProvider.defaults.useXDomain = true in your angular config block Commented Dec 24, 2014 at 14:08
  • 1
    It will failed because you use jQuery's style, not Angular's style of doing AJAX. Commented Dec 24, 2014 at 14:09
  • better-inter.net/enabling-cors-in-angular-js Commented Dec 25, 2014 at 10:16

4 Answers 4

1
$http.get('http://test.wilson34.com/test.php', {params: {name: 'peter'}
}).success(function(data, status, headers, config) {
  console.log('Success' + data);
}).error(function(data, status, headers, config) {
  console.log('Status : ' + status);
});

Params is an object with key, value pairs. In this case it evaluates to ?name=peter

Sign up to request clarification or add additional context in comments.

Comments

1

I think that you are using the $http service in a wrong way. $http() is service, not a function you need to call the get() or post() method from this service:

$http.get('http://test.wilson34.com/test.php?name=peter').
success(function(data, status, headers, config) {
    console.log(data);
}).
error(function(data, status, headers, config) {
    console.log('Status : ' + status);
});

Checkout the documentation.

1 Comment

yes sure i know that and i was using $http.get but i just typed it wrong in my question thank for advice , but it not working
0

Basically your syntax for $http is wrong.
Use it like a service is used in angular js and don't forget to use $http as a parameter to your method which hosts $http request

$http.get(URL)
.success(function(response) {
    console.log('success');
})
.error(function(response){
    console.log('error')
});

Comments

0

thank you all , byrdr said i was have to put the data in a param object that solved it

final code is :

$http({
            url : 'http://test.wilson34.com/test.php',
            type : 'GET',
            params : {
                'name' : encrypted
            }
        }).success(function(data, status, headers, config) {
            // this callback will be called asynchronously
            // when the response is available
            console.log('Success' + data);
            alert(data);
        }).error(function(data, status, headers, config) {
            console.log('Status : ' + status);
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.