Error with AJAX in JavaScript for Cross Domain ASMX Web Service JSONP Call -
i have locally running web service believe meets required criteria cross-domain javascript calls accessing external data within dynamics crm, , running errors creating javascript ajax code access external web service.
and can cause results shown in screen shot 1 below appear accessing web service @ http://aloyegeneraltest1/returnjson.asmx/getpricejson
my problem unable figure out how write javascript code serialized items shown in web service above.
when run page below, , click "test" button, error stating 0x800a1391 - javascript runtime error: 'getjsonp' undefined.
<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="clientsidegeneraltest._default" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "www.w3.org/.../xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <script language="javascript" type="text/javascript" src="scripts/jquery-3.1.1.min.js"> function getjsonp() { debugger; $.ajax({ url: "aloyegeneraltest1/.../getpricejson", type: "post", contenttype: "application/json; charset=utf-8", data: '{"name":' + json.stringify(getdata()) + '}' }).done(function(result) { alert(result.d); }).fail(function(result) { alert(result.d); }); } } </script> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <input id="button1" type="button" value="test" onclick="getjsonp()" /><br /> </div> </form> </body> </html>
if remove jquery reference entirely, eliminates undefined function error above, doing causes new unhandled exception error shown - 0x800a1391 - javascript runtime error: '$' undefined
the modified code produces new error looks this:
<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="clientsidegeneraltest._default" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "www.w3.org/.../xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <script language="javascript" type="text/javascript"> function getjsonp() { debugger; $.ajax({ url: "aloyegeneraltest1/.../getpricejson", type: "post", contenttype: "application/json; charset=utf-8", data: '{"name":' + json.stringify(getdata()) + '}' }).done(function(result) { alert(result.d); }).fail(function(result) { alert(result.d); }); } </script> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <input id="button1" type="button" value="test" onclick="getjsonp()" /><br /> </div> </form> </body> </html>
it appears having kind of issue $ @ start of ajax code.
i new ajax , new development in general or advise may able provide appreciated.
may these lines of code solve problem
var jsondata = [your json parameter]; $.ajax({ async: false, type: "post", url: [your web service url], contenttype: "application/json; charset=utf-8", data: json.stringify({ json: jsondata }), datatype: "json", success: onsuccess, failure: function(err) { alert("error : " + err.d); } }); function onsuccess(data) { alert("success:" + data.d); }
you can 1 thing need set access-control-allow-origin & access-control-allow-headers in customeheaders web service web.config file.
<add name="access-control-allow-origin" value="*" /> <add name="access-control-allow-headers" value="content-type" />
if want allow specific domain , can specific value of domain instead of * value
Comments
Post a Comment