Rest web services and jquery ajax method i used i got error -
i got below error xmlhttprequest cannot load http://localhost/assessment/api.php/useradd
. no 'access-control-allow-origin' header present on requested resource. origin 'null' therefore not allowed access.
i use below code please me fix issue
var serviceurl = "`http://localhost/assessment/api.php/`"; $("#useraddbuttonid").click(function() { $('#userfrmid').submit(); }); $('#userfrmid').submit(function(){ //alert('hi'); var postdata = $(this).serialize(); //postdata="username=test"; // postdata = { username: "john", email: "boston" }; //alert(postdata); $.ajax({ type: 'post', data: postdata, url: serviceurl+'useradd', success: function(data){ alert(data); if(data.response == 1) { console.log(data); alert("user added successfully"); window.localstorage.setitem('form_active','#addrfrmid'); // store local storage window.location.href='index.html'; }else{ console.log(data); alert(data.response); } }, error: function(data){alert(data); console.log(data); alert('there error adding comment'); } }); return false; }); api.php function useradd_post(){ $this->load->model('user_model'); $datas['username']=$this->input->post('username'); $datas['email']=$this->input->post('email'); $datas['phone']=$this->input->post('phone'); $this->user = new user(); $ret = $this->user->web_user_insert($datas); if($ret) { $this->response($ret, 200); // 200 being http response code } else { $this->response(null, 404); } }
javascript ajax limited making ajax calls within same domain. in case, looks intention. might make sense change service url relative... code assume same domain. use /assessment/api.php/useradd.
if that's not case you, it's still fixable. service need changed handle options request being fired before post check access-control-allow-origin header. needs return header part of response. don't know enough php out there. looks there's info here...
https://developer.mozilla.org/en-us/docs/web/http/server-side_access_control
Comments
Post a Comment