0

I've made a test fiddle here: http://jsfiddle.net/BjLdQ/2

Where I can enter the number 1 for each HTML field and then I press the login button and I get the JSON back as:

{"code":"2"}

So in my mind, I should be able to do the same with jQuery so I press the (Login Jquery Ajax) text and nothing happens, in jsfiddle or from my local machine?

Any help would be most appreciated.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Bill
  • 4,614
  • 13
  • 77
  • 132
  • Did you actually JSLint your code before you submitted this question? Your fiddle has errors. – Brian Driscoll Mar 20 '12 at 14:29
  • Also, I get this error in the console: `XMLHttpRequest cannot load https://api.searchningbo.com/user/login. Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin.` – Brian Driscoll Mar 20 '12 at 14:31

2 Answers2

5

Same Origin Policy... You cannot fetch pages from a different origin (MDN).

See Ways to circumvent the same-origin policy for work-arounds.

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • So if i put my javascript on the domain https://api.searchningbo.com all will be ok? Do I need to have the html, javascript and jquery files all on the same domain? – Bill Mar 20 '12 at 14:54
  • @Bill The origin is determined by the location of the (HTML) page. Place your html file at `hhtps://api.searchningbo.com/` (same PROTOCOL, `https` and DOMAIN `api.searchningbo.com`), and the error will disappear. – Rob W Mar 20 '12 at 16:28
1

There is an error in the code.

$("#button").click(function() {
     alert("Sending");

      $.ajax({
            type: "POST",
            dataType: 'JSON',
            data: "{'username':'1','password':'1','key':'1','device_Id':'1'}",
            url: "https://api.searchningbo.com/user/login",
            // This was here: },
            success: function(data) {
                alert(data);          

            }

        });
} // It wasnt closed properly   
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Gerson Beltrán
  • 402
  • 4
  • 8