jquery - 404 when calling my wcf service in javascript - what am I missing? -
i trying call wcf service , using jquery's ajax. unless mistaken problem seems when try data in json format. unsure if url correct.
this service.svc.cs. want call addnewquery
//------------------------------------------------------------------------------ // <copyright file="webdataservice.svc.cs" company="microsoft"> // copyright (c) microsoft corporation. rights reserved. // </copyright> //------------------------------------------------------------------------------ using system; using system.collections.generic; using system.data.services; using system.data.services.common; using system.linq; using system.servicemodel.web; using system.web; using system.net; using system.data.objects; using system.data.entity; using system.net.http; using system.web.http; using system.web; using system.servicemodel.web; namespace bradapi { [jsonpsupportbehavior] [system.servicemodel.servicebehavior(includeexceptiondetailinfaults = true)] public class service1 : system.data.services.dataservice<bradonlineentities> { // method called once initialize service-wide policies. public static void initializeservice(dataserviceconfiguration config) { httpcontext.current.response.addheader("access-control-allow-origin", "*"); config.setentitysetaccessrule("*", entitysetrights.all); config.useverboseerrors = true; config.setserviceoperationaccessrule("addnewquery", serviceoperationrights.all); config.setserviceoperationaccessrule("getquerybyid", serviceoperationrights.all); config.dataservicebehavior.maxprotocolversion = dataserviceprotocolversion.v3; } protected override void onstartprocessingrequest(processrequestargs args) { base.onstartprocessingrequest(args); //cache minute based on querystring httpcontext context = httpcontext.current; httpcachepolicy c = httpcontext.current.response.cache; c.setcacheability(httpcacheability.serverandprivate); c.setexpires(httpcontext.current.timestamp.addseconds(60)); c.varybyheaders["accept"] = true; c.varybyheaders["accept-charset"] = true; c.varybyheaders["accept-encoding"] = true; c.varybyparams["*"] = true; } [webget] public iqueryable<tblcontactquery> getquerybyid(guid queryid) { iqueryable<tblcontactquery> biglist = (from c in this.currentdatasource.tblcontactqueries c.queryid.equals(queryid) select c); return biglist; } [webget] public iqueryable<tblcontactquery> addnewquery(string querytext, string userid) { // make null remove compile errors guid guserid = guid.parse(userid); guid queryid = guid.newguid(); tblcontactquery c = new tblcontactquery { queryid = queryid, userid = guserid, querytext = querytext, }; try { this.currentdatasource.tblcontactqueries.add(c); this.currentdatasource.savechanges(); return getquerybyid(queryid); } catch { return getquerybyid(queryid); } } } } 

my js code
function sendquery() { userid = $("#hdnuserid").val(); contactid = $("#hdncontactid").val(); txtquery = $('#txtquery').val(); var successflag; $.ajax({ url: url, data: "userid='" + userid + "'&" + "querytext='" + txtquery + "'&" + "contactid='" + contactid + "'", type: "get", async: false, datatype: "json", success: function (data) { successflag = 1; }, error: function (data) { successflag == 0; } }); if (successflag == 1) { alert("thank you, query has been sent member of alf team"); $("#dialog").css("display", "false"); $("#dialog").dialog("close"); } else if (successflag == 0) { alert("query not sent alf team"); $("#dialog").css("display", "false"); $("#dialog").dialog("close"); } } my success , error block don't hit because it's not hitting service. it's not success or failure.
any tips changing url?
url value parameters aren't delimited ' '. use encodeuricomponent(...) correctly escape parameters.
instead of
data: "userid='" + userid + "'&" + "querytext='" + txtquery + "'&" + "contactid='" + contactid + "'" try
data: "userid=" + userid + "&querytext=" + encodeuricomponent(txtquery) + "&contactid=" + encodeuricomponent(contactid), additionally url seems wrong, according 1 of screenshots. in http://i.stack.imgur.com/ktflv.png url http://stating1......com/alfapi/service.svc/addnewquery?format=json?querytext.
this indicates url variable contains query, i.e. http://stating1......com/alfapi/service.svc/addnewquery?format=json (the ?format=json part query within url paramater should avoid when using data $.ajax(...).
instead change
var url = 'http://stating1......com/alfapi/service.svc/addnewquery'; .... $.ajax({ url: url, // move format=json url variable data: part data: "format=json&userid=" + encodeuricomponent(userid) + "&querytext=" + encodeuricomponent(txtquery) + "&contactid=" + encodeuricomponent(contactid), type: "get", async: false, datatype: "json", success: function (data) { successflag = 1; }, error: function (data) { successflag == 0; } });
Comments
Post a Comment