javascript - AJAX POST in .NET -


i have below javascript ajax request works:

$.ajax({     type: "post",     url: requesturl,     data: {         var1: 'value1',         var2: 'value2',         var3: 'value3'     },     xhrfields: {         withcredentials: true     },     datatype: "json",     crossdomain: true,     complete: function (jqxhr, textstatus) {         if (jqxhr !== undefined && jqxhr !== null && jqxhr.statustext === "ok") {             alert("request success" + jqxhr.responsetext);         } else {             alert("request failed" + (jqxhr.responsetext || textstatus));         }      } }); 

i same using .net.

i tried following:

string result = string.empty; try {     using (var client = new webclient())     {         client.credentials = new networkcredential("username", "password");          var values = new namevaluecollection();         values["var1"] = "value1";         values["var2"] = "value2";         values["var3"] = "value3";          var response = client.uploadvalues(requesturl, values);          result = encoding.default.getstring(response);      } } catch (exception exception) {     console.writeline("error=" + exception.message); }  console.writeline("result=" + result); 

however internal server error.

how recreate cross domain request? there specific header set?

webexception details:

response: "the remote server returned error: (500) internal server error." headers: persistent-auth: true content-length: 5143 cache-control: private content-type: text/html; charset=utf-8 date: tue, 14 jul 2015 12:03:21 gmt server: microsoft-iis/7.5 x-aspnet-version: 4.0.30319 x-powered-by: asp.net

without seeing error it's hard what's going on if suspect cors issue can add these headers:

var data = jsonconvert.serializeobject(     new     {         prop1 = 1,         prop2 = "data 2"     });  using (var client = new webclient { usedefaultcredentials = true }) {     client.headers.add("access-control-allow-origin", "*");     client.headers.add("access-control-allow-methods", "get, post, options");     client.headers.add("access-control-allow-headers", "authorization, content-type, accept");     client.headers.add(httprequestheader.contenttype, "application/json; charset=utf-8");     client.uploaddata("your url", "post", encoding.utf8.getbytes(data)); } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -