A
A
Anton Natarov2017-02-24 18:43:15
JavaScript
Anton Natarov, 2017-02-24 18:43:15

JSONP how to organize getting html in callback?

Good afternoon, the situation is as follows: I want to build a registration form on my third-party site. Since I don't want to mess around with CORS, I decided to make requests via jsonp get. The apishka itself receives data in the controller. Depending on them, it builds a form and displays it through the view along with the entire layout Html page. The form itself already sends data for registration (transferring data via jsonp without a response through the callback works properly).
Connecting a script on a third-party site

<script id="jsonp_script" src="https://somesite.com/assets/js/api/test.js?login=1&email=1&password=1"></script>

The controller request
var res = $.ajax({
      url: "https://somesite.com/test",
      method: "GET",
      data: fields, //массив из полей, которые были true 
      dataType: "jsonp",
      jsonp: "callback",
      contentType: "application/json; charset=utf-8",
          success:function(response){
              $("#some").append(response);;
          }
    });    
    
    res.done(function( msg ) {
        alert(msg);
    });

But when I receive it, I get the error
SyntaxError: expected expression, got '<'
I looked at the debugger and what it refers to. There is a normal html code and swears at the initial first quote. Those. the answer itself is already coming, but as it starts to stick, it immediately throws an error. How it is possible to receive Html page all the same?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Kucherov, 2017-02-24
@DexterHD

I understand correctly, the response comes with HTML? If so, then therein lies the problem.
You indicated that you are expecting a jsonp response from the server , but you are receiving HTML.
If you need to get HTML, the dataType must be set to html
. In general, I would recommend getting confused with CORS, everything is not as complicated as it might seem.
https://learn.javascript.ru/xhr-crossdomain

I
Immortal_pony, 2017-02-24
@Immortal_pony

Return json object from server side, not bare html.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question