asp.net - ajax call to asp web servers gives is not allowed by Access-Control-Allow-Origin. error -


i created function ajax call localhost webservers

and gets error message console. origin not allowed access-control-allow-origin. web.config

  <system.webserver>      <modules runallmanagedmodulesforallrequests="true"/>      <httpprotocol>       <customheaders>         <add name="access-control-allow-origin" value="*" />         <add name="access-control-allow-headers" value="content-type" />       </customheaders>     </httpprotocol>    </system.webserver> 

this js function:

function doajax() { $.ajax({       type: "post",       contenttype: "application/json; charset=utf-8",       url: "http://localhost:5181/webservice1.asmx/updatereport",       data: "{'test_description':'" + testcasediscription + "', "            + "'test_id':'" + id + "',"            + "'tester_name':'" + testername + "',"            + "'test_url':'" + test_url + "'}",      datatype: "json",     error: function (err){                  alert(err)         },      success: function (data){                     alert(data)     }  }); } 

and webservis

[webmethod] public string updatereport(string test_description, string test_id, string test_url, string tester_name) {     report report = new report();      report.test_description = test_description;     report.test_id = test_id;     report.test_url = test_url;     report.tester_name = tester_name;       if (report.updatedatabase())         return "correct";      return "sorry"; } 

any ideas why not working? have been reading issue appear beucase cross domain ajax calls. have no idea how fix it.

any ideas?

access-control-allow-origin not server side issue, client. can't make calls content different domain...including different security level ie ssl or sub domain or socket!.

there work around though, jsonp. allows call script execute call function. response sever needs either wrapped in callback function sent in request or if use jquery, lt handle dirty work you. see example

       `jquery.ajax({         datatype: 'jsonp',         data: payload,         url: "http://someotherdomain.com?callback=?",         jsonp: true})         .done(function (response, status, request) {             if (response.success)                 processresponse(response.success);             console.log("done");         })         .fail(function (response, status, request) {             console.log("fail");          })         .always(function (response) {             console.log("always");          })` 

server side php

$response = array("key" => 'a message'); $success = $success?"success":"failure"; $response = json_encode($response); echo $_get['callback'].'(' . "{\"".$success ."\" : ".$response."}" . ')'; 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -