0

I POST the values to my server using jQuery (API call). The values are succesfully added to the db but I don't get any response from the server.

Here is my jquery for post .

$(document).ready(function () {
    $('#signupConfirm').click(function () {
        if (validateSignUpForm()) {
            //check the availability of username.

            // post all these vars into the API call
            var signUpServiceUrl = 'http://xxxxxxxxxxxxxxxx';
            var apiKey = 'xxxxxxxxxxxxxx';
            var apiSecret = 'xxxxxxxxxxxxxxxxxxxx';
            var userName = $('#username').val();
            var email = $('#email1').val();
            var password = $('#password1').val();

            $.post(signUpServiceUrl, {
                    'api_key': apiKey,
                    'api_secret': apiSecret,
                    'username': userName,
                    'email': email,
                    'password': password
            }, function (data) {
                result = JSON.parse(data);
            });
        }
    });

});

Is there any mistake in this?

Spokey
  • 10,974
  • 2
  • 28
  • 44
user3789039
  • 93
  • 1
  • 1
  • 12
  • 1
    grab the error using this and debug further http://stackoverflow.com/a/2833968/953684 – Sharky Oct 17 '14 at 10:06
  • check browser dev tools console for errors. Also check network section to watch server request/response values. – aleha_84 Oct 17 '14 at 10:08
  • After doing the above ^ please show an example of the returned JSON. Are you sure it's valid? Is this a cross-domain request? What are you doing with result and why is it global? – Spokey Oct 17 '14 at 10:10
  • @spokey this is the sample respone that i expect {"header":{"status":"200","status_text":"OK"},"data":{"id_user":"38"}} – user3789039 Oct 17 '14 at 10:32
  • @spokey this will not get in the jquery. – user3789039 Oct 17 '14 at 10:32
  • It will go in `result`, are you sure it's not there? Any console error messages? – Spokey Oct 17 '14 at 10:33
  • what are you sending in response? – Apul Gupta Oct 17 '14 at 10:44
  • @Spokey The console says this .. Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://xxxxxxxxxxxxxxxxxxxxxxxx. This can be fixed by moving the resource to the same domain or enabling CORS. , fbcdn-profile-a.akamaihd.net : server does not support RFC 5746, see CVE-2009-3555 . As a new person i don't understand what it is. will u please explain. – user3789039 Oct 17 '14 at 10:47
  • @Apul Gupta. I send the response like this. {"header":{"status":"200","status_text":"OK"},"data":{"id_user":"38"}} – user3789039 Oct 17 '14 at 10:48
  • You are sending request to another domain or sub-domain, that's why getting cross-origin request error. right? – Apul Gupta Oct 17 '14 at 10:50
  • Yes, you are not allowed to send requests like this to another domain. Try putting jsonp at the end of post like this and try again `function (data) { alert(data) }, 'jsonp');` if you do this there is no need to parse again, probably – Spokey Oct 17 '14 at 10:52
  • @Spokey i added 'jsonp' at the end of the post but it will return an empty alert box.. $.post(signUpServiceUrl,{'api_key':apiKey,'api_secret':apiSecret,'username':userName,'email':email,'password':password},function(data){ alert(data),'jsonp'; }); – user3789039 Oct 17 '14 at 11:07
  • open your browser console and at the network tab look at the headers when you are sending it. See what you get if you go to that page. The request now reaches the server, you now have to debug it. See the sent data if it's right or not. – Spokey Oct 17 '14 at 11:55
  • it give the status code for OK (200). But the response tab here is empty.. – user3789039 Oct 17 '14 at 12:12

1 Answers1

0

You are sending cross-domain requests, use jsonp. Here is an example:

http://hayageek.com/cross-domain-ajax-request-jquery/

Apul Gupta
  • 3,044
  • 3
  • 22
  • 30
  • i added jsonp at the end of the post but it will not return any thing... Alert box returns only blank. – user3789039 Oct 17 '14 at 12:02
  • I have not suggested to use `jsonp` in the end of `$.post`. Try the example given in answer. http://hayageek.com/cross-domain-ajax-request-jquery/ – Apul Gupta Oct 17 '14 at 12:08