javascript - jQuery .post is working but triggering .fail without any information -
i've got data posting i'm not having luck getting .post response handler code work. inconsistent results in different browsers/tools i've tried. here's post code:
$.post(form.attr("action"), form.serialize(), "json") .done(function (response, textstatus, jqxhr) { alert('done'); }) .fail(function (jqxhr, textstatus, errorthrown) { // log error console alert('responsetext:' + jqxhr.responsetext + ', status:' + textstatus + ', error:' + errorthrown); })
in firefox , chrome goes .fail (even though data posting) item set textstatus "error". in firefox when try view response shows error, "syntaxerror: json.parse: unexpected end of data @ line 1 column 1". in chrome, in console i'm seeing this: "xmlhttprequest cannot load http://example.net/applicationsvc/formprocessor/index.php. no 'access-control-allow-origin' header present on requested resource. origin 'http://example.net' therefore not allowed access." seems relevant attempts solve haven't worked.
how can resolve access-control-allow-origin issue in .post? why aren't getting error data , why firefox unable parse response.
using postman, , using same headers , body, see i'm getting response of:
{"successful":true,"thankyou_message":"<h2>thank you!<\/h2><p>thank signing up!<\/p>"}
but code doesn't seem getting or handling that.
here request headers going out:
host: example.net user-agent: mozilla/5.0 (windows nt 6.3; wow64; rv:38.0) gecko/20100101 firefox/38.0 accept: */* accept-language: en-us,en;q=0.5 accept-encoding: gzip, deflate content-type: application/x-www-form-urlencoded; charset=utf-8 referer: http://example.net/mypage.htm content-length: 655 origin: http://example.net connection: keep-alive pragma: no-cache cache-control: no-cache
update: i've switched .post .ajax
$.ajax({ url: form.attr("action"), type: "post", data: form.serialize(), datatype: "json", contenttype: "application/json; charset=utf-8", success: function () { alert('done'); } });
with consistent http 501 response.
try enable cross-origin on server, example in php
header("access-control-allow-origin: *");
the server must send in response header, change star accordingly domain security reasons.
Comments
Post a Comment